File: scanner.l

package info (click to toggle)
pscan 1.2-9
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 116 kB
  • ctags: 43
  • sloc: ansic: 310; lex: 141; makefile: 17; sh: 1
file content (141 lines) | stat: -rw-r--r-- 5,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
%option yylineno

%{
#include "pscan.h"
static void skip_strings(char literal);
%}

%x comment
%x strings
  
reserved    "default"|"struct"|"void"|"for"|"if"|"else"|"while"|"do"|"return"|"case"|"switch"|"break"|"auto"|"continue"|"goto"|"sizeof"|"static"|"typedef"|"union"|"volatile"

vartype "char"|"double"|"enum"|"extern"|"float"|"int"|"long"|"register"|"short"|"signed"|"unsigned"|"const"      

cprep "include"|"define"|"if"|"else"|"endif"|"ifdef"|"ifndef"

%%
{reserved}                            state->last_token = NOT_PROBLEMATIC;

{vartype}                             state->last_token = NOT_PROBLEMATIC;


"#"{cprep}                            state->last_token = NOT_PROBLEMATIC;

NULL				      { if ((state->last_token == PROBLEMATIC) &&
                                          (state->constant_string < 0)) {
                                         state->constant_string = state->args;
                                         }
                                       }

\"                                    {
                                          if (state->last_token == PROBLEMATIC) {
                                          if (state->constant_string < 0) {
                                          state->constant_string = state->args;
                                          }
}
                                          skip_strings('"');
                                      }

\'                                    skip_strings('\'');

\/\/.*$                               /* skip C++ style comments */

[a-zA-Z_][_a-zA-Z0-9]*                state = setup_checks(yytext, state);

[ \t]+                                /* eat up whitespace */

\(                                    {
                                        if (state->args < 0) state->args = 0;
                                        state->braces++;
                                        if (state->braces > 1) {
                                          state = push_stack(state);
                                          state->last_token = NOT_PROBLEMATIC;
                                          state->braces = 1;
                                          state->args = -1;
                                        }
                                      }

\,                                    if (state->last_token == PROBLEMATIC) {
                                        if (state->braces != 0) {
                                          state->args++;
                                        } else {
                                          state->last_token = NOT_PROBLEMATIC;
                                        }
                                      }

\)                                    if (state->last_token == PROBLEMATIC) {
                                        check_function(state);
                                        state->last_token = NOT_PROBLEMATIC;
                                      } else if (state->braces != 0) {
                                        state->braces--;
                                        if (state->braces == 0) {
                                          state = pop_stack();
                                        }
                                      }

.                                     {
                                        if ((state->last_token == PROBLEMATIC) &&
                                             (state->braces == 0)) {
                                          state->last_token = NOT_PROBLEMATIC;
                                        }
                                      }

                                      
"\n"|"\r"          		      /* ignore LF's and CR's */


"/*"    BEGIN(comment);

<comment>[^*\n]*        /* eat anything that's not a '*' */
<comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<comment>\n             /* do nothing */
<comment>"*"+"/"        BEGIN(INITIAL);

%%
/**********************************************************************
 * pscan: http://www.striker.ottawa.on.ca/~aland/pscan/
 *
 * Copyright (C) 2000 Alan DeKok <aland@ox.org>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 **********************************************************************/
static const char rcsid[] = "$Id: scanner.l,v 1.4 2000/07/17 18:51:45 aland Exp $";

/* static */
void skip_strings(char literal)
{
       int c;

       while ((c = input()) != literal)
           {
           switch (c) {
            
           case '\\':
                      c = input(); 
                      if (c == '\\') continue;
                      if (c == EOF) return;
                      if (c != literal)
                        unput(c);
                      break;
           case EOF:
                return;

           default:
                      break;
            }
          }
}