├── Lexical Analysis ├── a.out ├── input.c ├── lex.yy.c ├── output.txt └── prog.l ├── Semantic Analysis ├── a.out ├── absprog.y ├── lex.yy.c ├── out.txt ├── output.txt ├── prog.l ├── prog.y └── y.tab.c ├── Syntax Analysis ├── a.out ├── input.c ├── lex.yy.c ├── prog.l ├── prog.y └── y.tab.c └── readme /Lexical Analysis/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShamithaUdupa/Simple-C-Compiler/6055a89791ac4e3f77cf2347162b00a8a48dcba7/Lexical Analysis/a.out -------------------------------------------------------------------------------- /Lexical Analysis/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* This is a multi 3 | line comment*/ 4 | int main() { 5 | int amount, rate, time, si, 56ee; 6 | //This is a single line comment 7 | printf("\nEnter Principal Amount : "); 8 | scanf("%d", &amount); 9 | 10 | printf("\nEnter Rate of Interest : "); 11 | scanf("%d", &rate); 12 | 13 | printf("\nEnter Period of Time : "); 14 | scanf("%d", &time); 15 | scanf("",); 16 | si = (amount * rate * time) / 100; 17 | printf("\nSimple Interest : %d", si); 18 | printf("",); 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /Lexical Analysis/output.txt: -------------------------------------------------------------------------------- 1 | Lexeme Token Value 2 | #include Preprocessor Statement 0 3 | int Keyword 1 4 | main Identifier 2 5 | ( Punctuator 3 6 | ) Punctuator 4 7 | { Punctuator 5 8 | int Keyword 6 9 | amount Identifier 7 10 | , Punctuator 8 11 | rate Identifier 9 12 | , Punctuator 10 13 | time Identifier 11 14 | , Punctuator 12 15 | si Identifier 13 16 | , Punctuator 14 17 | ; Punctuator 15 18 | ; Punctuator 16 19 | ; Punctuator 17 20 | ; Punctuator 18 21 | ; Punctuator 19 22 | ; Punctuator 20 23 | ; Punctuator 21 24 | ; Punctuator 22 25 | si Identifier 23 26 | = Operator 24 27 | ( Punctuator 25 28 | amount Identifier 26 29 | * Operator 27 30 | rate Identifier 28 31 | * Operator 29 32 | time Identifier 30 33 | ) Punctuator 31 34 | / Operator 32 35 | 100 Constant 33 36 | ; Punctuator 34 37 | ; Punctuator 35 38 | ; Punctuator 36 39 | return Keyword 37 40 | ( Punctuator 38 41 | 0 Constant 39 42 | ) Punctuator 40 43 | ; Punctuator 41 44 | } Punctuator 42 45 | -------------------------------------------------------------------------------- /Lexical Analysis/prog.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | #include 6 | void insert(char*,char); 7 | int countentry=0; 8 | int comment=0; 9 | int bracketcount=0; 10 | %} 11 | 12 | alpha [a-zA-Z] 13 | digit [0-9] 14 | exp [Ee][+|-]?{digit}+ 15 | string {alpha}?\"(\\.|[^\\"])*\" 16 | printf (printf\({string}(\,([^,)])+)*\)) 17 | prnterr (printf\(.*\)) 18 | scanf (scanf\({string}(\,([^,)])+)*\)) 19 | scanerr (scanf\(.*\)) 20 | %x mcomment 21 | 22 | %% 23 | "//".* ; 24 | "-"|"+"|"*"|"/"|">"|"<"|"="|"&"|"|"|"^"|"<="|">="|"!="|"=="|"&&"|"||"|"%"|"~"|"++"|"--" {insert(yytext,'o');} 25 | "+="|"-="|"*="|"/="|"<<="|">>=" {insert(yytext,'o');} 26 | ^#.* {insert(yytext,'d');} 27 | {printf} {;} 28 | {prnterr} {printf("ERROR: Printf statement\n");} 29 | {scanf} {;} 30 | {scanerr} {printf("ERROR: Scanf statement\n");} 31 | {string} {insert(yytext,'s');} 32 | {alpha}?\"(\\.|[^\\"])* {printf("ERROR: String Literal\n");} 33 | "switch"|"if"|"else"|"break"|"char"|"int"|"while"|"float"|"double"|"return" {insert(yytext,'k');} 34 | "case"|"const"|"continue"|"default"|"do"|"extern"|"for"|"goto"|"long"|"short" {insert(yytext,'k');} 35 | "signed"|"sizeof"|"static"|"struct"|"typedef"|"union"|"void"|"unsigned" {insert(yytext,'k');} 36 | "/*" {BEGIN(mcomment);} 37 | \*\/ {BEGIN(INITIAL);} 38 | . ; 39 | \n ; 40 | {alpha}({alpha}|{digit})* {insert(yytext,'v');} 41 | {digit}+ {insert(yytext,'c');} 42 | {digit}+{exp} {insert(yytext,'c');} 43 | {digit}+{alpha}+ {printf("ERROR : Bad token %s\n",yytext);} 44 | {digit}+"."{digit}*{exp}? {insert(yytext,'c');} 45 | {digit}*"."{digit}+{exp}? {insert(yytext,'c');} 46 | [ \t\n] ; 47 | . {insert(yytext,'p');} 48 | %% 49 | 50 | void insert(char* yytext,char type) 51 | { 52 | switch(type) 53 | { 54 | case 'v': 55 | fprintf(yyout,"\t%s\t\t\tIdentifier\t\t\t%d\n",yytext,countentry); 56 | countentry++; 57 | break; 58 | case 'c': 59 | fprintf(yyout,"\t%s\t\t\tConstant\t\t\t%d\n",yytext,countentry); 60 | countentry++; 61 | break; 62 | case 'p': 63 | fprintf(yyout,"\t%s\t\t\tPunctuator\t\t\t%d\n",yytext,countentry); 64 | countentry++; 65 | break; 66 | case 'o': 67 | fprintf(yyout,"\t%s\t\t\tOperator\t\t\t%d\n",yytext,countentry); 68 | countentry++; 69 | break; 70 | case 'k': 71 | fprintf(yyout,"\t%s\t\t\tKeyword\t\t\t%d\n",yytext,countentry); 72 | countentry++; 73 | break; 74 | case 's': 75 | fprintf(yyout,"\t%s\t\t\tString Literal\t\t\t%d\n",yytext,countentry); 76 | countentry++; 77 | break; 78 | case 'd': 79 | fprintf(yyout,"\t%s\t\t\tPreprocessor Statement\t\t\t%d\n",yytext,countentry); 80 | countentry++; 81 | break; 82 | } 83 | } 84 | 85 | int yywrap() 86 | { 87 | return 1; 88 | } 89 | 90 | int main() 91 | { 92 | yyin=fopen("input.c","r"); 93 | yyout=fopen("output.txt","w"); 94 | fprintf(yyout,"\tLexeme\t\t\tToken\t\t\tValue\n"); 95 | yylex(); 96 | fclose(yyout); 97 | fclose(yyin); 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /Semantic Analysis/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShamithaUdupa/Simple-C-Compiler/6055a89791ac4e3f77cf2347162b00a8a48dcba7/Semantic Analysis/a.out -------------------------------------------------------------------------------- /Semantic Analysis/absprog.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | struct node 6 | { 7 | struct node* left; 8 | struct node* right; 9 | char* token; 10 | }; 11 | struct node* mknode(struct node* left,struct node* right,char* token); 12 | void printtree(struct node* tree); 13 | #define YYSTYPE struct node* 14 | FILE* yyout; 15 | %} 16 | 17 | %token IDENTIFIER NUM HEADER REAL CHARVALUE 18 | %token CHAR INT FLOAT DOUBLE VOID 19 | %token EQ LE GE AND OR XOR ASSIGN L G NEQ 20 | %token IF ELSE SWITCH BREAK WHILE CASE DEFAULT RETURN 21 | %token ADD SUB MUL DIV INC DEC 22 | %token SEMICOLON COMMA 23 | %token OP CP OB CB 24 | 25 | %start program 26 | 27 | %% 28 | program 29 | : header programnext {printf("Input accepted\n");exit(0);} 30 | ; 31 | header 32 | : HEADER 33 | | HEADER header 34 | ; 35 | programnext 36 | : declarations 37 | | function 38 | | declarations function 39 | | function function 40 | ; 41 | returnstatement 42 | : RETURN expressions SEMICOLON 43 | | {;} 44 | ; 45 | declarations 46 | : type assignmentlist SEMICOLON 47 | ; 48 | assignmentlist 49 | : variable ASSIGN number 50 | | variable COMMA assignmentlist 51 | | variable 52 | ; 53 | number 54 | : NUM 55 | | REAL 56 | | CHARVALUE 57 | ; 58 | type 59 | : INT 60 | | CHAR 61 | | FLOAT 62 | | DOUBLE 63 | | VOID 64 | ; 65 | function 66 | : type IDENTIFIER OP argumentlist CP OB statements returnstatement CB 67 | | function function 68 | | {;} 69 | ; 70 | variable 71 | : IDENTIFIER 72 | ; 73 | argument 74 | : type variable 75 | ; 76 | argumentlist 77 | : argument 78 | | argument COMMA argumentlist 79 | | {;} 80 | ; 81 | id 82 | : IDENTIFIER 83 | ; 84 | statements 85 | : id ASSIGN expressions SEMICOLON {printtree($3);fprintf(yyout,"\n");} 86 | | type variable ASSIGN expressions SEMICOLON {printtree($4);fprintf(yyout,"\n");} 87 | | ifstatements 88 | | switchstatements 89 | | whilestatements 90 | | statements statements 91 | ; 92 | ifstatements 93 | : IF OP conditionalexpressions CP OB statements CB ELSE OB statements CB 94 | ; 95 | switchstatements 96 | : SWITCH OP id CP OB casestatements CB 97 | ; 98 | whilestatements 99 | : WHILE OP conditionalexpressions CP OB statements CB 100 | ; 101 | casestatements 102 | : CASE NUM ':' statements BREAK SEMICOLON casestatements 103 | | DEFAULT ':' statements BREAK SEMICOLON 104 | | {;} 105 | ; 106 | expressions 107 | : conditionalexpressions {$$=$1;} 108 | | expressions ADD expressions {$$=mknode($1,$3,"+");} 109 | | expressions SUB expressions {$$=mknode($1,$3,"-");} 110 | | expressions MUL expressions {$$=mknode($1,$3,"*");} 111 | | expressions DIV expressions {$$=mknode($1,$3,"/");} 112 | | IDENTIFIER {$$=mknode(0,0,(char*)yylval);} 113 | | NUM {$$=mknode(0,0,(char*)yylval);} 114 | | OP expressions CP {$$=$2;} 115 | | INC IDENTIFIER {$$=mknode($2,0,"++");} 116 | | DEC IDENTIFIER {$$=mknode($2,0,"--");} 117 | | REAL {$$=mknode(0,0,(char*)yylval);} 118 | | CHARVALUE {$$=mknode(0,0,(char*)yylval);} 119 | ; 120 | conditionalexpressions 121 | : expressions AND expressions {$$=mknode($1,$3,"AND");} 122 | | expressions OR expressions {$$=mknode($1,$3,"OR");} 123 | | expressions LE expressions {$$=mknode($1,$3,"LE");} 124 | | expressions L expressions {$$=mknode($1,$3,"L");} 125 | | expressions G expressions {$$=mknode($1,$3,"G");} 126 | | expressions NEQ expressions {$$=mknode($1,$3,"NEQ");} 127 | | expressions GE expressions {$$=mknode($1,$3,"GE");} 128 | | expressions EQ expressions {$$=mknode($1,$3,"EQ");} 129 | | OP conditionalexpressions CP {$$=$2;} 130 | ; 131 | %% 132 | 133 | struct node* mknode(struct node* left,struct node* right,char* token) 134 | { 135 | struct node* new=(struct node*)malloc(sizeof(struct node)); 136 | char* newstr=(char*)malloc(strlen(token)+1); 137 | strcpy(newstr,token); 138 | new->left=left; 139 | new->right=right; 140 | new->token=newstr; 141 | return new; 142 | } 143 | 144 | void printtree(struct node* tree) 145 | { 146 | if(tree->left||tree->right) 147 | fprintf(yyout,"("); 148 | fprintf(yyout," %s ",tree->token); 149 | if(tree->left) 150 | printtree(tree->left); 151 | if(tree->right) 152 | printtree(tree->right); 153 | if(tree->left||tree->right) 154 | fprintf(yyout,")"); 155 | } 156 | 157 | #include "lex.yy.c" 158 | int main() 159 | { 160 | yyout=fopen("output.txt","w"); 161 | fprintf(yyout,"Abstract Syntax Tree\n"); 162 | yyparse(); 163 | fclose(yyout); 164 | return 0; 165 | } 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /Semantic Analysis/lex.yy.c: -------------------------------------------------------------------------------- 1 | 2 | #line 3 "lex.yy.c" 3 | 4 | #define YY_INT_ALIGNED short int 5 | 6 | /* A lexical scanner generated by flex */ 7 | 8 | #define FLEX_SCANNER 9 | #define YY_FLEX_MAJOR_VERSION 2 10 | #define YY_FLEX_MINOR_VERSION 5 11 | #define YY_FLEX_SUBMINOR_VERSION 39 12 | #if YY_FLEX_SUBMINOR_VERSION > 0 13 | #define FLEX_BETA 14 | #endif 15 | 16 | /* First, we deal with platform-specific or compiler-specific issues. */ 17 | 18 | /* begin standard C headers. */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | /* end standard C headers. */ 25 | 26 | /* flex integer type definitions */ 27 | 28 | #ifndef FLEXINT_H 29 | #define FLEXINT_H 30 | 31 | /* C99 systems have . Non-C99 systems may or may not. */ 32 | 33 | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 34 | 35 | /* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, 36 | * if you want the limit (max/min) macros for int types. 37 | */ 38 | #ifndef __STDC_LIMIT_MACROS 39 | #define __STDC_LIMIT_MACROS 1 40 | #endif 41 | 42 | #include 43 | typedef int8_t flex_int8_t; 44 | typedef uint8_t flex_uint8_t; 45 | typedef int16_t flex_int16_t; 46 | typedef uint16_t flex_uint16_t; 47 | typedef int32_t flex_int32_t; 48 | typedef uint32_t flex_uint32_t; 49 | #else 50 | typedef signed char flex_int8_t; 51 | typedef short int flex_int16_t; 52 | typedef int flex_int32_t; 53 | typedef unsigned char flex_uint8_t; 54 | typedef unsigned short int flex_uint16_t; 55 | typedef unsigned int flex_uint32_t; 56 | 57 | /* Limits of integral types. */ 58 | #ifndef INT8_MIN 59 | #define INT8_MIN (-128) 60 | #endif 61 | #ifndef INT16_MIN 62 | #define INT16_MIN (-32767-1) 63 | #endif 64 | #ifndef INT32_MIN 65 | #define INT32_MIN (-2147483647-1) 66 | #endif 67 | #ifndef INT8_MAX 68 | #define INT8_MAX (127) 69 | #endif 70 | #ifndef INT16_MAX 71 | #define INT16_MAX (32767) 72 | #endif 73 | #ifndef INT32_MAX 74 | #define INT32_MAX (2147483647) 75 | #endif 76 | #ifndef UINT8_MAX 77 | #define UINT8_MAX (255U) 78 | #endif 79 | #ifndef UINT16_MAX 80 | #define UINT16_MAX (65535U) 81 | #endif 82 | #ifndef UINT32_MAX 83 | #define UINT32_MAX (4294967295U) 84 | #endif 85 | 86 | #endif /* ! C99 */ 87 | 88 | #endif /* ! FLEXINT_H */ 89 | 90 | #ifdef __cplusplus 91 | 92 | /* The "const" storage-class-modifier is valid. */ 93 | #define YY_USE_CONST 94 | 95 | #else /* ! __cplusplus */ 96 | 97 | /* C99 requires __STDC__ to be defined as 1. */ 98 | #if defined (__STDC__) 99 | 100 | #define YY_USE_CONST 101 | 102 | #endif /* defined (__STDC__) */ 103 | #endif /* ! __cplusplus */ 104 | 105 | #ifdef YY_USE_CONST 106 | #define yyconst const 107 | #else 108 | #define yyconst 109 | #endif 110 | 111 | /* Returned upon end-of-file. */ 112 | #define YY_NULL 0 113 | 114 | /* Promotes a possibly negative, possibly signed char to an unsigned 115 | * integer for use as an array index. If the signed char is negative, 116 | * we want to instead treat it as an 8-bit unsigned char, hence the 117 | * double cast. 118 | */ 119 | #define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) 120 | 121 | /* Enter a start condition. This macro really ought to take a parameter, 122 | * but we do it the disgusting crufty way forced on us by the ()-less 123 | * definition of BEGIN. 124 | */ 125 | #define BEGIN (yy_start) = 1 + 2 * 126 | 127 | /* Translate the current start state into a value that can be later handed 128 | * to BEGIN to return to the state. The YYSTATE alias is for lex 129 | * compatibility. 130 | */ 131 | #define YY_START (((yy_start) - 1) / 2) 132 | #define YYSTATE YY_START 133 | 134 | /* Action number for EOF rule of a given start state. */ 135 | #define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) 136 | 137 | /* Special action meaning "start processing a new file". */ 138 | #define YY_NEW_FILE yyrestart(yyin ) 139 | 140 | #define YY_END_OF_BUFFER_CHAR 0 141 | 142 | /* Size of default input buffer. */ 143 | #ifndef YY_BUF_SIZE 144 | #ifdef __ia64__ 145 | /* On IA-64, the buffer size is 16k, not 8k. 146 | * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. 147 | * Ditto for the __ia64__ case accordingly. 148 | */ 149 | #define YY_BUF_SIZE 32768 150 | #else 151 | #define YY_BUF_SIZE 16384 152 | #endif /* __ia64__ */ 153 | #endif 154 | 155 | /* The state buf must be large enough to hold one state per character in the main buffer. 156 | */ 157 | #define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) 158 | 159 | #ifndef YY_TYPEDEF_YY_BUFFER_STATE 160 | #define YY_TYPEDEF_YY_BUFFER_STATE 161 | typedef struct yy_buffer_state *YY_BUFFER_STATE; 162 | #endif 163 | 164 | #ifndef YY_TYPEDEF_YY_SIZE_T 165 | #define YY_TYPEDEF_YY_SIZE_T 166 | typedef size_t yy_size_t; 167 | #endif 168 | 169 | extern yy_size_t yyleng; 170 | 171 | extern FILE *yyin, *yyout; 172 | 173 | #define EOB_ACT_CONTINUE_SCAN 0 174 | #define EOB_ACT_END_OF_FILE 1 175 | #define EOB_ACT_LAST_MATCH 2 176 | 177 | #define YY_LESS_LINENO(n) 178 | #define YY_LINENO_REWIND_TO(ptr) 179 | 180 | /* Return all but the first "n" matched characters back to the input stream. */ 181 | #define yyless(n) \ 182 | do \ 183 | { \ 184 | /* Undo effects of setting up yytext. */ \ 185 | int yyless_macro_arg = (n); \ 186 | YY_LESS_LINENO(yyless_macro_arg);\ 187 | *yy_cp = (yy_hold_char); \ 188 | YY_RESTORE_YY_MORE_OFFSET \ 189 | (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ 190 | YY_DO_BEFORE_ACTION; /* set up yytext again */ \ 191 | } \ 192 | while ( 0 ) 193 | 194 | #define unput(c) yyunput( c, (yytext_ptr) ) 195 | 196 | #ifndef YY_STRUCT_YY_BUFFER_STATE 197 | #define YY_STRUCT_YY_BUFFER_STATE 198 | struct yy_buffer_state 199 | { 200 | FILE *yy_input_file; 201 | 202 | char *yy_ch_buf; /* input buffer */ 203 | char *yy_buf_pos; /* current position in input buffer */ 204 | 205 | /* Size of input buffer in bytes, not including room for EOB 206 | * characters. 207 | */ 208 | yy_size_t yy_buf_size; 209 | 210 | /* Number of characters read into yy_ch_buf, not including EOB 211 | * characters. 212 | */ 213 | yy_size_t yy_n_chars; 214 | 215 | /* Whether we "own" the buffer - i.e., we know we created it, 216 | * and can realloc() it to grow it, and should free() it to 217 | * delete it. 218 | */ 219 | int yy_is_our_buffer; 220 | 221 | /* Whether this is an "interactive" input source; if so, and 222 | * if we're using stdio for input, then we want to use getc() 223 | * instead of fread(), to make sure we stop fetching input after 224 | * each newline. 225 | */ 226 | int yy_is_interactive; 227 | 228 | /* Whether we're considered to be at the beginning of a line. 229 | * If so, '^' rules will be active on the next match, otherwise 230 | * not. 231 | */ 232 | int yy_at_bol; 233 | 234 | int yy_bs_lineno; /**< The line count. */ 235 | int yy_bs_column; /**< The column count. */ 236 | 237 | /* Whether to try to fill the input buffer when we reach the 238 | * end of it. 239 | */ 240 | int yy_fill_buffer; 241 | 242 | int yy_buffer_status; 243 | 244 | #define YY_BUFFER_NEW 0 245 | #define YY_BUFFER_NORMAL 1 246 | /* When an EOF's been seen but there's still some text to process 247 | * then we mark the buffer as YY_EOF_PENDING, to indicate that we 248 | * shouldn't try reading from the input source any more. We might 249 | * still have a bunch of tokens to match, though, because of 250 | * possible backing-up. 251 | * 252 | * When we actually see the EOF, we change the status to "new" 253 | * (via yyrestart()), so that the user can continue scanning by 254 | * just pointing yyin at a new input file. 255 | */ 256 | #define YY_BUFFER_EOF_PENDING 2 257 | 258 | }; 259 | #endif /* !YY_STRUCT_YY_BUFFER_STATE */ 260 | 261 | /* Stack of input buffers. */ 262 | static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ 263 | static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ 264 | static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ 265 | 266 | /* We provide macros for accessing buffer states in case in the 267 | * future we want to put the buffer states in a more general 268 | * "scanner state". 269 | * 270 | * Returns the top of the stack, or NULL. 271 | */ 272 | #define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ 273 | ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ 274 | : NULL) 275 | 276 | /* Same as previous macro, but useful when we know that the buffer stack is not 277 | * NULL or when we need an lvalue. For internal use only. 278 | */ 279 | #define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] 280 | 281 | /* yy_hold_char holds the character lost when yytext is formed. */ 282 | static char yy_hold_char; 283 | static yy_size_t yy_n_chars; /* number of characters read into yy_ch_buf */ 284 | yy_size_t yyleng; 285 | 286 | /* Points to current character in buffer. */ 287 | static char *yy_c_buf_p = (char *) 0; 288 | static int yy_init = 0; /* whether we need to initialize */ 289 | static int yy_start = 0; /* start state number */ 290 | 291 | /* Flag which is used to allow yywrap()'s to do buffer switches 292 | * instead of setting up a fresh yyin. A bit of a hack ... 293 | */ 294 | static int yy_did_buffer_switch_on_eof; 295 | 296 | void yyrestart (FILE *input_file ); 297 | void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); 298 | YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); 299 | void yy_delete_buffer (YY_BUFFER_STATE b ); 300 | void yy_flush_buffer (YY_BUFFER_STATE b ); 301 | void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); 302 | void yypop_buffer_state (void ); 303 | 304 | static void yyensure_buffer_stack (void ); 305 | static void yy_load_buffer_state (void ); 306 | static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); 307 | 308 | #define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) 309 | 310 | YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); 311 | YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); 312 | YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); 313 | 314 | void *yyalloc (yy_size_t ); 315 | void *yyrealloc (void *,yy_size_t ); 316 | void yyfree (void * ); 317 | 318 | #define yy_new_buffer yy_create_buffer 319 | 320 | #define yy_set_interactive(is_interactive) \ 321 | { \ 322 | if ( ! YY_CURRENT_BUFFER ){ \ 323 | yyensure_buffer_stack (); \ 324 | YY_CURRENT_BUFFER_LVALUE = \ 325 | yy_create_buffer(yyin,YY_BUF_SIZE ); \ 326 | } \ 327 | YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ 328 | } 329 | 330 | #define yy_set_bol(at_bol) \ 331 | { \ 332 | if ( ! YY_CURRENT_BUFFER ){\ 333 | yyensure_buffer_stack (); \ 334 | YY_CURRENT_BUFFER_LVALUE = \ 335 | yy_create_buffer(yyin,YY_BUF_SIZE ); \ 336 | } \ 337 | YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ 338 | } 339 | 340 | #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) 341 | 342 | /* Begin user sect3 */ 343 | 344 | typedef unsigned char YY_CHAR; 345 | 346 | FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; 347 | 348 | typedef int yy_state_type; 349 | 350 | extern int yylineno; 351 | 352 | int yylineno = 1; 353 | 354 | extern char *yytext; 355 | #define yytext_ptr yytext 356 | 357 | static yy_state_type yy_get_previous_state (void ); 358 | static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); 359 | static int yy_get_next_buffer (void ); 360 | static void yy_fatal_error (yyconst char msg[] ); 361 | 362 | /* Done after the current pattern has been matched and before the 363 | * corresponding action - sets up yytext. 364 | */ 365 | #define YY_DO_BEFORE_ACTION \ 366 | (yytext_ptr) = yy_bp; \ 367 | yyleng = (size_t) (yy_cp - yy_bp); \ 368 | (yy_hold_char) = *yy_cp; \ 369 | *yy_cp = '\0'; \ 370 | (yy_c_buf_p) = yy_cp; 371 | 372 | #define YY_NUM_RULES 44 373 | #define YY_END_OF_BUFFER 45 374 | /* This struct is not used in this scanner, 375 | but its presence is necessary. */ 376 | struct yy_trans_info 377 | { 378 | flex_int32_t yy_verify; 379 | flex_int32_t yy_nxt; 380 | }; 381 | static yyconst flex_int16_t yy_accept[110] = 382 | { 0, 383 | 0, 0, 45, 43, 42, 42, 43, 43, 43, 37, 384 | 38, 3, 2, 36, 1, 43, 4, 32, 35, 8, 385 | 11, 9, 31, 16, 31, 31, 31, 31, 31, 31, 386 | 31, 31, 31, 31, 39, 43, 40, 41, 10, 12, 387 | 0, 14, 15, 33, 34, 32, 6, 7, 5, 31, 388 | 31, 31, 31, 31, 31, 31, 31, 18, 31, 31, 389 | 31, 31, 31, 13, 41, 30, 0, 33, 0, 31, 390 | 31, 31, 31, 31, 31, 31, 22, 31, 31, 31, 391 | 31, 33, 0, 34, 31, 26, 21, 31, 31, 19, 392 | 31, 31, 31, 27, 31, 33, 20, 31, 31, 24, 393 | 394 | 31, 31, 23, 31, 29, 28, 17, 25, 0 395 | } ; 396 | 397 | static yyconst flex_int32_t yy_ec[256] = 398 | { 0, 399 | 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 400 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 401 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 402 | 1, 2, 4, 1, 5, 1, 1, 6, 7, 8, 403 | 9, 10, 11, 12, 13, 14, 15, 16, 16, 16, 404 | 16, 16, 16, 16, 16, 16, 16, 1, 17, 18, 405 | 19, 20, 1, 1, 21, 21, 21, 21, 22, 21, 406 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 407 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 408 | 1, 1, 1, 23, 1, 1, 24, 25, 26, 27, 409 | 410 | 28, 29, 21, 30, 31, 21, 32, 33, 21, 34, 411 | 35, 21, 21, 36, 37, 38, 39, 40, 41, 21, 412 | 21, 21, 42, 43, 44, 1, 1, 1, 1, 1, 413 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 414 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 415 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 416 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 417 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 418 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 419 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 420 | 421 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 422 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 423 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 424 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 425 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 426 | 1, 1, 1, 1, 1 427 | } ; 428 | 429 | static yyconst flex_int32_t yy_meta[45] = 430 | { 0, 431 | 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 432 | 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 433 | 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 434 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 435 | 3, 1, 1, 1 436 | } ; 437 | 438 | static yyconst flex_int16_t yy_base[113] = 439 | { 0, 440 | 0, 123, 127, 129, 129, 129, 107, 119, 0, 129, 441 | 129, 129, 113, 129, 110, 106, 129, 31, 129, 102, 442 | 101, 100, 0, 129, 82, 22, 20, 84, 83, 20, 443 | 87, 73, 78, 82, 129, 68, 129, 0, 129, 129, 444 | 103, 129, 129, 34, 35, 44, 129, 129, 129, 0, 445 | 81, 71, 83, 77, 66, 67, 68, 0, 64, 63, 446 | 69, 68, 67, 129, 0, 129, 81, 37, 80, 71, 447 | 66, 57, 68, 66, 62, 65, 0, 49, 49, 59, 448 | 52, 68, 67, 66, 49, 0, 0, 41, 46, 0, 449 | 40, 41, 50, 0, 47, 58, 0, 40, 44, 0, 450 | 451 | 36, 37, 0, 26, 0, 0, 0, 0, 129, 65, 452 | 58, 68 453 | } ; 454 | 455 | static yyconst flex_int16_t yy_def[113] = 456 | { 0, 457 | 109, 1, 109, 109, 109, 109, 109, 109, 110, 109, 458 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 459 | 109, 109, 111, 109, 111, 111, 111, 111, 111, 111, 460 | 111, 111, 111, 111, 109, 109, 109, 112, 109, 109, 461 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 111, 462 | 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 463 | 111, 111, 111, 109, 112, 109, 109, 109, 109, 111, 464 | 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 465 | 111, 109, 109, 109, 111, 111, 111, 111, 111, 111, 466 | 111, 111, 111, 111, 111, 109, 111, 111, 111, 111, 467 | 468 | 111, 111, 111, 111, 111, 111, 111, 111, 0, 109, 469 | 109, 109 470 | } ; 471 | 472 | static yyconst flex_int16_t yy_nxt[174] = 473 | { 0, 474 | 4, 5, 6, 7, 4, 8, 9, 10, 11, 12, 475 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 476 | 23, 23, 24, 23, 25, 26, 27, 28, 29, 23, 477 | 30, 23, 23, 23, 23, 31, 32, 23, 23, 33, 478 | 34, 35, 36, 37, 45, 52, 46, 54, 58, 44, 479 | 68, 53, 68, 59, 55, 67, 69, 45, 83, 46, 480 | 50, 67, 69, 108, 83, 41, 107, 41, 65, 106, 481 | 65, 105, 104, 96, 103, 102, 101, 100, 99, 98, 482 | 97, 84, 96, 82, 95, 94, 93, 92, 91, 90, 483 | 89, 88, 87, 86, 85, 84, 82, 81, 80, 79, 484 | 485 | 78, 77, 76, 75, 74, 73, 72, 71, 70, 66, 486 | 64, 63, 62, 61, 60, 57, 56, 51, 49, 48, 487 | 47, 44, 43, 42, 40, 39, 109, 38, 3, 109, 488 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 489 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 490 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 491 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 492 | 109, 109, 109 493 | } ; 494 | 495 | static yyconst flex_int16_t yy_chk[174] = 496 | { 0, 497 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 498 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 499 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 500 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 501 | 1, 1, 1, 1, 18, 26, 18, 27, 30, 44, 502 | 45, 26, 68, 30, 27, 44, 45, 46, 68, 46, 503 | 111, 44, 45, 104, 68, 110, 102, 110, 112, 101, 504 | 112, 99, 98, 96, 95, 93, 92, 91, 89, 88, 505 | 85, 84, 83, 82, 81, 80, 79, 78, 76, 75, 506 | 74, 73, 72, 71, 70, 69, 67, 63, 62, 61, 507 | 508 | 60, 59, 57, 56, 55, 54, 53, 52, 51, 41, 509 | 36, 34, 33, 32, 31, 29, 28, 25, 22, 21, 510 | 20, 16, 15, 13, 8, 7, 3, 2, 109, 109, 511 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 512 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 513 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 514 | 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 515 | 109, 109, 109 516 | } ; 517 | 518 | static yy_state_type yy_last_accepting_state; 519 | static char *yy_last_accepting_cpos; 520 | 521 | extern int yy_flex_debug; 522 | int yy_flex_debug = 0; 523 | 524 | /* The intent behind this definition is that it'll catch 525 | * any uses of REJECT which flex missed. 526 | */ 527 | #define REJECT reject_used_but_not_detected 528 | #define yymore() yymore_used_but_not_detected 529 | #define YY_MORE_ADJ 0 530 | #define YY_RESTORE_YY_MORE_OFFSET 531 | char *yytext; 532 | #line 1 "prog.l" 533 | #line 2 "prog.l" 534 | #include 535 | #include 536 | #include 537 | #include 538 | #line 539 "lex.yy.c" 539 | 540 | #define INITIAL 0 541 | 542 | #ifndef YY_NO_UNISTD_H 543 | /* Special case for "unistd.h", since it is non-ANSI. We include it way 544 | * down here because we want the user's section 1 to have been scanned first. 545 | * The user has a chance to override it with an option. 546 | */ 547 | #include 548 | #endif 549 | 550 | #ifndef YY_EXTRA_TYPE 551 | #define YY_EXTRA_TYPE void * 552 | #endif 553 | 554 | static int yy_init_globals (void ); 555 | 556 | /* Accessor methods to globals. 557 | These are made visible to non-reentrant scanners for convenience. */ 558 | 559 | int yylex_destroy (void ); 560 | 561 | int yyget_debug (void ); 562 | 563 | void yyset_debug (int debug_flag ); 564 | 565 | YY_EXTRA_TYPE yyget_extra (void ); 566 | 567 | void yyset_extra (YY_EXTRA_TYPE user_defined ); 568 | 569 | FILE *yyget_in (void ); 570 | 571 | void yyset_in (FILE * in_str ); 572 | 573 | FILE *yyget_out (void ); 574 | 575 | void yyset_out (FILE * out_str ); 576 | 577 | yy_size_t yyget_leng (void ); 578 | 579 | char *yyget_text (void ); 580 | 581 | int yyget_lineno (void ); 582 | 583 | void yyset_lineno (int line_number ); 584 | 585 | /* Macros after this point can all be overridden by user definitions in 586 | * section 1. 587 | */ 588 | 589 | #ifndef YY_SKIP_YYWRAP 590 | #ifdef __cplusplus 591 | extern "C" int yywrap (void ); 592 | #else 593 | extern int yywrap (void ); 594 | #endif 595 | #endif 596 | 597 | static void yyunput (int c,char *buf_ptr ); 598 | 599 | #ifndef yytext_ptr 600 | static void yy_flex_strncpy (char *,yyconst char *,int ); 601 | #endif 602 | 603 | #ifdef YY_NEED_STRLEN 604 | static int yy_flex_strlen (yyconst char * ); 605 | #endif 606 | 607 | #ifndef YY_NO_INPUT 608 | 609 | #ifdef __cplusplus 610 | static int yyinput (void ); 611 | #else 612 | static int input (void ); 613 | #endif 614 | 615 | #endif 616 | 617 | /* Amount of stuff to slurp up with each read. */ 618 | #ifndef YY_READ_BUF_SIZE 619 | #ifdef __ia64__ 620 | /* On IA-64, the buffer size is 16k, not 8k */ 621 | #define YY_READ_BUF_SIZE 16384 622 | #else 623 | #define YY_READ_BUF_SIZE 8192 624 | #endif /* __ia64__ */ 625 | #endif 626 | 627 | /* Copy whatever the last rule matched to the standard output. */ 628 | #ifndef ECHO 629 | /* This used to be an fputs(), but since the string might contain NUL's, 630 | * we now use fwrite(). 631 | */ 632 | #define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0) 633 | #endif 634 | 635 | /* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, 636 | * is returned in "result". 637 | */ 638 | #ifndef YY_INPUT 639 | #define YY_INPUT(buf,result,max_size) \ 640 | if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ 641 | { \ 642 | int c = '*'; \ 643 | size_t n; \ 644 | for ( n = 0; n < max_size && \ 645 | (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ 646 | buf[n] = (char) c; \ 647 | if ( c == '\n' ) \ 648 | buf[n++] = (char) c; \ 649 | if ( c == EOF && ferror( yyin ) ) \ 650 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 651 | result = n; \ 652 | } \ 653 | else \ 654 | { \ 655 | errno=0; \ 656 | while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ 657 | { \ 658 | if( errno != EINTR) \ 659 | { \ 660 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 661 | break; \ 662 | } \ 663 | errno=0; \ 664 | clearerr(yyin); \ 665 | } \ 666 | }\ 667 | \ 668 | 669 | #endif 670 | 671 | /* No semi-colon after return; correct usage is to write "yyterminate();" - 672 | * we don't want an extra ';' after the "return" because that will cause 673 | * some compilers to complain about unreachable statements. 674 | */ 675 | #ifndef yyterminate 676 | #define yyterminate() return YY_NULL 677 | #endif 678 | 679 | /* Number of entries by which start-condition stack grows. */ 680 | #ifndef YY_START_STACK_INCR 681 | #define YY_START_STACK_INCR 25 682 | #endif 683 | 684 | /* Report a fatal error. */ 685 | #ifndef YY_FATAL_ERROR 686 | #define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) 687 | #endif 688 | 689 | /* end tables serialization structures and prototypes */ 690 | 691 | /* Default declaration of generated scanner - a define so the user can 692 | * easily add parameters. 693 | */ 694 | #ifndef YY_DECL 695 | #define YY_DECL_IS_OURS 1 696 | 697 | extern int yylex (void); 698 | 699 | #define YY_DECL int yylex (void) 700 | #endif /* !YY_DECL */ 701 | 702 | /* Code executed at the beginning of each rule, after yytext and yyleng 703 | * have been set up. 704 | */ 705 | #ifndef YY_USER_ACTION 706 | #define YY_USER_ACTION 707 | #endif 708 | 709 | /* Code executed at the end of each rule. */ 710 | #ifndef YY_BREAK 711 | #define YY_BREAK break; 712 | #endif 713 | 714 | #define YY_RULE_SETUP \ 715 | if ( yyleng > 0 ) \ 716 | YY_CURRENT_BUFFER_LVALUE->yy_at_bol = \ 717 | (yytext[yyleng - 1] == '\n'); \ 718 | YY_USER_ACTION 719 | 720 | /** The main scanner function which does all the work. 721 | */ 722 | YY_DECL 723 | { 724 | register yy_state_type yy_current_state; 725 | register char *yy_cp, *yy_bp; 726 | register int yy_act; 727 | 728 | if ( !(yy_init) ) 729 | { 730 | (yy_init) = 1; 731 | 732 | #ifdef YY_USER_INIT 733 | YY_USER_INIT; 734 | #endif 735 | 736 | if ( ! (yy_start) ) 737 | (yy_start) = 1; /* first start state */ 738 | 739 | if ( ! yyin ) 740 | yyin = stdin; 741 | 742 | if ( ! yyout ) 743 | yyout = stdout; 744 | 745 | if ( ! YY_CURRENT_BUFFER ) { 746 | yyensure_buffer_stack (); 747 | YY_CURRENT_BUFFER_LVALUE = 748 | yy_create_buffer(yyin,YY_BUF_SIZE ); 749 | } 750 | 751 | yy_load_buffer_state( ); 752 | } 753 | 754 | { 755 | #line 12 "prog.l" 756 | 757 | #line 758 "lex.yy.c" 758 | 759 | while ( 1 ) /* loops until end-of-file is reached */ 760 | { 761 | yy_cp = (yy_c_buf_p); 762 | 763 | /* Support of yytext. */ 764 | *yy_cp = (yy_hold_char); 765 | 766 | /* yy_bp points to the position in yy_ch_buf of the start of 767 | * the current run. 768 | */ 769 | yy_bp = yy_cp; 770 | 771 | yy_current_state = (yy_start); 772 | yy_current_state += YY_AT_BOL(); 773 | yy_match: 774 | do 775 | { 776 | register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; 777 | if ( yy_accept[yy_current_state] ) 778 | { 779 | (yy_last_accepting_state) = yy_current_state; 780 | (yy_last_accepting_cpos) = yy_cp; 781 | } 782 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 783 | { 784 | yy_current_state = (int) yy_def[yy_current_state]; 785 | if ( yy_current_state >= 110 ) 786 | yy_c = yy_meta[(unsigned int) yy_c]; 787 | } 788 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 789 | ++yy_cp; 790 | } 791 | while ( yy_base[yy_current_state] != 129 ); 792 | 793 | yy_find_action: 794 | yy_act = yy_accept[yy_current_state]; 795 | if ( yy_act == 0 ) 796 | { /* have to back up */ 797 | yy_cp = (yy_last_accepting_cpos); 798 | yy_current_state = (yy_last_accepting_state); 799 | yy_act = yy_accept[yy_current_state]; 800 | } 801 | 802 | YY_DO_BEFORE_ACTION; 803 | 804 | do_action: /* This label is used only to access EOF actions. */ 805 | 806 | switch ( yy_act ) 807 | { /* beginning of action switch */ 808 | case 0: /* must back up */ 809 | /* undo the effects of YY_DO_BEFORE_ACTION */ 810 | *yy_cp = (yy_hold_char); 811 | yy_cp = (yy_last_accepting_cpos); 812 | yy_current_state = (yy_last_accepting_state); 813 | goto yy_find_action; 814 | 815 | case 1: 816 | YY_RULE_SETUP 817 | #line 13 "prog.l" 818 | return SUB; 819 | YY_BREAK 820 | case 2: 821 | YY_RULE_SETUP 822 | #line 14 "prog.l" 823 | return ADD; 824 | YY_BREAK 825 | case 3: 826 | YY_RULE_SETUP 827 | #line 15 "prog.l" 828 | return MUL; 829 | YY_BREAK 830 | case 4: 831 | YY_RULE_SETUP 832 | #line 16 "prog.l" 833 | return DIV; 834 | YY_BREAK 835 | case 5: 836 | YY_RULE_SETUP 837 | #line 17 "prog.l" 838 | return GE; 839 | YY_BREAK 840 | case 6: 841 | YY_RULE_SETUP 842 | #line 18 "prog.l" 843 | return LE; 844 | YY_BREAK 845 | case 7: 846 | YY_RULE_SETUP 847 | #line 19 "prog.l" 848 | return EQ; 849 | YY_BREAK 850 | case 8: 851 | YY_RULE_SETUP 852 | #line 20 "prog.l" 853 | return L; 854 | YY_BREAK 855 | case 9: 856 | YY_RULE_SETUP 857 | #line 21 "prog.l" 858 | return G; 859 | YY_BREAK 860 | case 10: 861 | YY_RULE_SETUP 862 | #line 22 "prog.l" 863 | return NEQ; 864 | YY_BREAK 865 | case 11: 866 | YY_RULE_SETUP 867 | #line 23 "prog.l" 868 | {return ASSIGN;} 869 | YY_BREAK 870 | case 12: 871 | YY_RULE_SETUP 872 | #line 24 "prog.l" 873 | return AND; 874 | YY_BREAK 875 | case 13: 876 | YY_RULE_SETUP 877 | #line 25 "prog.l" 878 | return OR; 879 | YY_BREAK 880 | case 14: 881 | YY_RULE_SETUP 882 | #line 26 "prog.l" 883 | return INC; 884 | YY_BREAK 885 | case 15: 886 | YY_RULE_SETUP 887 | #line 27 "prog.l" 888 | return DEC; 889 | YY_BREAK 890 | case 16: 891 | YY_RULE_SETUP 892 | #line 28 "prog.l" 893 | return XOR; 894 | YY_BREAK 895 | case 17: 896 | YY_RULE_SETUP 897 | #line 29 "prog.l" 898 | return SWITCH; 899 | YY_BREAK 900 | case 18: 901 | YY_RULE_SETUP 902 | #line 30 "prog.l" 903 | return IF; 904 | YY_BREAK 905 | case 19: 906 | YY_RULE_SETUP 907 | #line 31 "prog.l" 908 | return ELSE; 909 | YY_BREAK 910 | case 20: 911 | YY_RULE_SETUP 912 | #line 32 "prog.l" 913 | return BREAK; 914 | YY_BREAK 915 | case 21: 916 | YY_RULE_SETUP 917 | #line 33 "prog.l" 918 | return CHAR; 919 | YY_BREAK 920 | case 22: 921 | YY_RULE_SETUP 922 | #line 34 "prog.l" 923 | return INT; 924 | YY_BREAK 925 | case 23: 926 | YY_RULE_SETUP 927 | #line 35 "prog.l" 928 | return WHILE; 929 | YY_BREAK 930 | case 24: 931 | YY_RULE_SETUP 932 | #line 36 "prog.l" 933 | return FLOAT; 934 | YY_BREAK 935 | case 25: 936 | YY_RULE_SETUP 937 | #line 37 "prog.l" 938 | return DEFAULT; 939 | YY_BREAK 940 | case 26: 941 | YY_RULE_SETUP 942 | #line 38 "prog.l" 943 | return CASE; 944 | YY_BREAK 945 | case 27: 946 | YY_RULE_SETUP 947 | #line 39 "prog.l" 948 | return VOID; 949 | YY_BREAK 950 | case 28: 951 | YY_RULE_SETUP 952 | #line 40 "prog.l" 953 | return RETURN; 954 | YY_BREAK 955 | case 29: 956 | YY_RULE_SETUP 957 | #line 41 "prog.l" 958 | return DOUBLE; 959 | YY_BREAK 960 | case 30: 961 | YY_RULE_SETUP 962 | #line 42 "prog.l" 963 | {yylval=(char*)yytext;return CHARVALUE;} 964 | YY_BREAK 965 | case 31: 966 | YY_RULE_SETUP 967 | #line 43 "prog.l" 968 | {yylval=(char*)yytext;return IDENTIFIER;} 969 | YY_BREAK 970 | case 32: 971 | YY_RULE_SETUP 972 | #line 44 "prog.l" 973 | {yylval=(char*)yytext;return NUM;} 974 | YY_BREAK 975 | case 33: 976 | YY_RULE_SETUP 977 | #line 45 "prog.l" 978 | {yylval=(char*)yytext;return REAL;} 979 | YY_BREAK 980 | case 34: 981 | YY_RULE_SETUP 982 | #line 46 "prog.l" 983 | {yylval=(char*)yytext;return REAL;} 984 | YY_BREAK 985 | case 35: 986 | YY_RULE_SETUP 987 | #line 47 "prog.l" 988 | return SEMICOLON; 989 | YY_BREAK 990 | case 36: 991 | YY_RULE_SETUP 992 | #line 48 "prog.l" 993 | return COMMA; 994 | YY_BREAK 995 | case 37: 996 | YY_RULE_SETUP 997 | #line 49 "prog.l" 998 | return OP; 999 | YY_BREAK 1000 | case 38: 1001 | YY_RULE_SETUP 1002 | #line 50 "prog.l" 1003 | return CP; 1004 | YY_BREAK 1005 | case 39: 1006 | YY_RULE_SETUP 1007 | #line 51 "prog.l" 1008 | return OB; 1009 | YY_BREAK 1010 | case 40: 1011 | YY_RULE_SETUP 1012 | #line 52 "prog.l" 1013 | return CB; 1014 | YY_BREAK 1015 | case 41: 1016 | YY_RULE_SETUP 1017 | #line 53 "prog.l" 1018 | {yylval=(char*)yytext;return HEADER;} 1019 | YY_BREAK 1020 | case 42: 1021 | /* rule 42 can match eol */ 1022 | YY_RULE_SETUP 1023 | #line 54 "prog.l" 1024 | ; 1025 | YY_BREAK 1026 | case 43: 1027 | YY_RULE_SETUP 1028 | #line 55 "prog.l" 1029 | return yytext; 1030 | YY_BREAK 1031 | case 44: 1032 | YY_RULE_SETUP 1033 | #line 56 "prog.l" 1034 | ECHO; 1035 | YY_BREAK 1036 | #line 1037 "lex.yy.c" 1037 | case YY_STATE_EOF(INITIAL): 1038 | yyterminate(); 1039 | 1040 | case YY_END_OF_BUFFER: 1041 | { 1042 | /* Amount of text matched not including the EOB char. */ 1043 | int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; 1044 | 1045 | /* Undo the effects of YY_DO_BEFORE_ACTION. */ 1046 | *yy_cp = (yy_hold_char); 1047 | YY_RESTORE_YY_MORE_OFFSET 1048 | 1049 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) 1050 | { 1051 | /* We're scanning a new file or input source. It's 1052 | * possible that this happened because the user 1053 | * just pointed yyin at a new source and called 1054 | * yylex(). If so, then we have to assure 1055 | * consistency between YY_CURRENT_BUFFER and our 1056 | * globals. Here is the right place to do so, because 1057 | * this is the first action (other than possibly a 1058 | * back-up) that will match for the new input source. 1059 | */ 1060 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 1061 | YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; 1062 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; 1063 | } 1064 | 1065 | /* Note that here we test for yy_c_buf_p "<=" to the position 1066 | * of the first EOB in the buffer, since yy_c_buf_p will 1067 | * already have been incremented past the NUL character 1068 | * (since all states make transitions on EOB to the 1069 | * end-of-buffer state). Contrast this with the test 1070 | * in input(). 1071 | */ 1072 | if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) 1073 | { /* This was really a NUL. */ 1074 | yy_state_type yy_next_state; 1075 | 1076 | (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; 1077 | 1078 | yy_current_state = yy_get_previous_state( ); 1079 | 1080 | /* Okay, we're now positioned to make the NUL 1081 | * transition. We couldn't have 1082 | * yy_get_previous_state() go ahead and do it 1083 | * for us because it doesn't know how to deal 1084 | * with the possibility of jamming (and we don't 1085 | * want to build jamming into it because then it 1086 | * will run more slowly). 1087 | */ 1088 | 1089 | yy_next_state = yy_try_NUL_trans( yy_current_state ); 1090 | 1091 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1092 | 1093 | if ( yy_next_state ) 1094 | { 1095 | /* Consume the NUL. */ 1096 | yy_cp = ++(yy_c_buf_p); 1097 | yy_current_state = yy_next_state; 1098 | goto yy_match; 1099 | } 1100 | 1101 | else 1102 | { 1103 | yy_cp = (yy_c_buf_p); 1104 | goto yy_find_action; 1105 | } 1106 | } 1107 | 1108 | else switch ( yy_get_next_buffer( ) ) 1109 | { 1110 | case EOB_ACT_END_OF_FILE: 1111 | { 1112 | (yy_did_buffer_switch_on_eof) = 0; 1113 | 1114 | if ( yywrap( ) ) 1115 | { 1116 | /* Note: because we've taken care in 1117 | * yy_get_next_buffer() to have set up 1118 | * yytext, we can now set up 1119 | * yy_c_buf_p so that if some total 1120 | * hoser (like flex itself) wants to 1121 | * call the scanner after we return the 1122 | * YY_NULL, it'll still work - another 1123 | * YY_NULL will get returned. 1124 | */ 1125 | (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; 1126 | 1127 | yy_act = YY_STATE_EOF(YY_START); 1128 | goto do_action; 1129 | } 1130 | 1131 | else 1132 | { 1133 | if ( ! (yy_did_buffer_switch_on_eof) ) 1134 | YY_NEW_FILE; 1135 | } 1136 | break; 1137 | } 1138 | 1139 | case EOB_ACT_CONTINUE_SCAN: 1140 | (yy_c_buf_p) = 1141 | (yytext_ptr) + yy_amount_of_matched_text; 1142 | 1143 | yy_current_state = yy_get_previous_state( ); 1144 | 1145 | yy_cp = (yy_c_buf_p); 1146 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1147 | goto yy_match; 1148 | 1149 | case EOB_ACT_LAST_MATCH: 1150 | (yy_c_buf_p) = 1151 | &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; 1152 | 1153 | yy_current_state = yy_get_previous_state( ); 1154 | 1155 | yy_cp = (yy_c_buf_p); 1156 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1157 | goto yy_find_action; 1158 | } 1159 | break; 1160 | } 1161 | 1162 | default: 1163 | YY_FATAL_ERROR( 1164 | "fatal flex scanner internal error--no action found" ); 1165 | } /* end of action switch */ 1166 | } /* end of scanning one token */ 1167 | } /* end of user's declarations */ 1168 | } /* end of yylex */ 1169 | 1170 | /* yy_get_next_buffer - try to read in a new buffer 1171 | * 1172 | * Returns a code representing an action: 1173 | * EOB_ACT_LAST_MATCH - 1174 | * EOB_ACT_CONTINUE_SCAN - continue scanning from current position 1175 | * EOB_ACT_END_OF_FILE - end of file 1176 | */ 1177 | static int yy_get_next_buffer (void) 1178 | { 1179 | register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; 1180 | register char *source = (yytext_ptr); 1181 | register int number_to_move, i; 1182 | int ret_val; 1183 | 1184 | if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) 1185 | YY_FATAL_ERROR( 1186 | "fatal flex scanner internal error--end of buffer missed" ); 1187 | 1188 | if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) 1189 | { /* Don't try to fill the buffer, so this is an EOF. */ 1190 | if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) 1191 | { 1192 | /* We matched a single character, the EOB, so 1193 | * treat this as a final EOF. 1194 | */ 1195 | return EOB_ACT_END_OF_FILE; 1196 | } 1197 | 1198 | else 1199 | { 1200 | /* We matched some text prior to the EOB, first 1201 | * process it. 1202 | */ 1203 | return EOB_ACT_LAST_MATCH; 1204 | } 1205 | } 1206 | 1207 | /* Try to read more data. */ 1208 | 1209 | /* First move last chars to start of buffer. */ 1210 | number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; 1211 | 1212 | for ( i = 0; i < number_to_move; ++i ) 1213 | *(dest++) = *(source++); 1214 | 1215 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) 1216 | /* don't do the read, it's not guaranteed to return an EOF, 1217 | * just force an EOF 1218 | */ 1219 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; 1220 | 1221 | else 1222 | { 1223 | yy_size_t num_to_read = 1224 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; 1225 | 1226 | while ( num_to_read <= 0 ) 1227 | { /* Not enough room in the buffer - grow it. */ 1228 | 1229 | /* just a shorter name for the current buffer */ 1230 | YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; 1231 | 1232 | int yy_c_buf_p_offset = 1233 | (int) ((yy_c_buf_p) - b->yy_ch_buf); 1234 | 1235 | if ( b->yy_is_our_buffer ) 1236 | { 1237 | yy_size_t new_size = b->yy_buf_size * 2; 1238 | 1239 | if ( new_size <= 0 ) 1240 | b->yy_buf_size += b->yy_buf_size / 8; 1241 | else 1242 | b->yy_buf_size *= 2; 1243 | 1244 | b->yy_ch_buf = (char *) 1245 | /* Include room in for 2 EOB chars. */ 1246 | yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); 1247 | } 1248 | else 1249 | /* Can't grow it, we don't own it. */ 1250 | b->yy_ch_buf = 0; 1251 | 1252 | if ( ! b->yy_ch_buf ) 1253 | YY_FATAL_ERROR( 1254 | "fatal error - scanner input buffer overflow" ); 1255 | 1256 | (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; 1257 | 1258 | num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - 1259 | number_to_move - 1; 1260 | 1261 | } 1262 | 1263 | if ( num_to_read > YY_READ_BUF_SIZE ) 1264 | num_to_read = YY_READ_BUF_SIZE; 1265 | 1266 | /* Read in more data. */ 1267 | YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), 1268 | (yy_n_chars), num_to_read ); 1269 | 1270 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1271 | } 1272 | 1273 | if ( (yy_n_chars) == 0 ) 1274 | { 1275 | if ( number_to_move == YY_MORE_ADJ ) 1276 | { 1277 | ret_val = EOB_ACT_END_OF_FILE; 1278 | yyrestart(yyin ); 1279 | } 1280 | 1281 | else 1282 | { 1283 | ret_val = EOB_ACT_LAST_MATCH; 1284 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = 1285 | YY_BUFFER_EOF_PENDING; 1286 | } 1287 | } 1288 | 1289 | else 1290 | ret_val = EOB_ACT_CONTINUE_SCAN; 1291 | 1292 | if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { 1293 | /* Extend the array by 50%, plus the number we really need. */ 1294 | yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); 1295 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); 1296 | if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) 1297 | YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); 1298 | } 1299 | 1300 | (yy_n_chars) += number_to_move; 1301 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; 1302 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; 1303 | 1304 | (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; 1305 | 1306 | return ret_val; 1307 | } 1308 | 1309 | /* yy_get_previous_state - get the state just before the EOB char was reached */ 1310 | 1311 | static yy_state_type yy_get_previous_state (void) 1312 | { 1313 | register yy_state_type yy_current_state; 1314 | register char *yy_cp; 1315 | 1316 | yy_current_state = (yy_start); 1317 | yy_current_state += YY_AT_BOL(); 1318 | 1319 | for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) 1320 | { 1321 | register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); 1322 | if ( yy_accept[yy_current_state] ) 1323 | { 1324 | (yy_last_accepting_state) = yy_current_state; 1325 | (yy_last_accepting_cpos) = yy_cp; 1326 | } 1327 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1328 | { 1329 | yy_current_state = (int) yy_def[yy_current_state]; 1330 | if ( yy_current_state >= 110 ) 1331 | yy_c = yy_meta[(unsigned int) yy_c]; 1332 | } 1333 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1334 | } 1335 | 1336 | return yy_current_state; 1337 | } 1338 | 1339 | /* yy_try_NUL_trans - try to make a transition on the NUL character 1340 | * 1341 | * synopsis 1342 | * next_state = yy_try_NUL_trans( current_state ); 1343 | */ 1344 | static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) 1345 | { 1346 | register int yy_is_jam; 1347 | register char *yy_cp = (yy_c_buf_p); 1348 | 1349 | register YY_CHAR yy_c = 1; 1350 | if ( yy_accept[yy_current_state] ) 1351 | { 1352 | (yy_last_accepting_state) = yy_current_state; 1353 | (yy_last_accepting_cpos) = yy_cp; 1354 | } 1355 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1356 | { 1357 | yy_current_state = (int) yy_def[yy_current_state]; 1358 | if ( yy_current_state >= 110 ) 1359 | yy_c = yy_meta[(unsigned int) yy_c]; 1360 | } 1361 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1362 | yy_is_jam = (yy_current_state == 109); 1363 | 1364 | return yy_is_jam ? 0 : yy_current_state; 1365 | } 1366 | 1367 | static void yyunput (int c, register char * yy_bp ) 1368 | { 1369 | register char *yy_cp; 1370 | 1371 | yy_cp = (yy_c_buf_p); 1372 | 1373 | /* undo effects of setting up yytext */ 1374 | *yy_cp = (yy_hold_char); 1375 | 1376 | if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) 1377 | { /* need to shift things up to make room */ 1378 | /* +2 for EOB chars. */ 1379 | register yy_size_t number_to_move = (yy_n_chars) + 2; 1380 | register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ 1381 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; 1382 | register char *source = 1383 | &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; 1384 | 1385 | while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) 1386 | *--dest = *--source; 1387 | 1388 | yy_cp += (int) (dest - source); 1389 | yy_bp += (int) (dest - source); 1390 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = 1391 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; 1392 | 1393 | if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) 1394 | YY_FATAL_ERROR( "flex scanner push-back overflow" ); 1395 | } 1396 | 1397 | *--yy_cp = (char) c; 1398 | 1399 | (yytext_ptr) = yy_bp; 1400 | (yy_hold_char) = *yy_cp; 1401 | (yy_c_buf_p) = yy_cp; 1402 | } 1403 | 1404 | #ifndef YY_NO_INPUT 1405 | #ifdef __cplusplus 1406 | static int yyinput (void) 1407 | #else 1408 | static int input (void) 1409 | #endif 1410 | 1411 | { 1412 | int c; 1413 | 1414 | *(yy_c_buf_p) = (yy_hold_char); 1415 | 1416 | if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) 1417 | { 1418 | /* yy_c_buf_p now points to the character we want to return. 1419 | * If this occurs *before* the EOB characters, then it's a 1420 | * valid NUL; if not, then we've hit the end of the buffer. 1421 | */ 1422 | if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) 1423 | /* This was really a NUL. */ 1424 | *(yy_c_buf_p) = '\0'; 1425 | 1426 | else 1427 | { /* need more input */ 1428 | yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); 1429 | ++(yy_c_buf_p); 1430 | 1431 | switch ( yy_get_next_buffer( ) ) 1432 | { 1433 | case EOB_ACT_LAST_MATCH: 1434 | /* This happens because yy_g_n_b() 1435 | * sees that we've accumulated a 1436 | * token and flags that we need to 1437 | * try matching the token before 1438 | * proceeding. But for input(), 1439 | * there's no matching to consider. 1440 | * So convert the EOB_ACT_LAST_MATCH 1441 | * to EOB_ACT_END_OF_FILE. 1442 | */ 1443 | 1444 | /* Reset buffer status. */ 1445 | yyrestart(yyin ); 1446 | 1447 | /*FALLTHROUGH*/ 1448 | 1449 | case EOB_ACT_END_OF_FILE: 1450 | { 1451 | if ( yywrap( ) ) 1452 | return EOF; 1453 | 1454 | if ( ! (yy_did_buffer_switch_on_eof) ) 1455 | YY_NEW_FILE; 1456 | #ifdef __cplusplus 1457 | return yyinput(); 1458 | #else 1459 | return input(); 1460 | #endif 1461 | } 1462 | 1463 | case EOB_ACT_CONTINUE_SCAN: 1464 | (yy_c_buf_p) = (yytext_ptr) + offset; 1465 | break; 1466 | } 1467 | } 1468 | } 1469 | 1470 | c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ 1471 | *(yy_c_buf_p) = '\0'; /* preserve yytext */ 1472 | (yy_hold_char) = *++(yy_c_buf_p); 1473 | 1474 | YY_CURRENT_BUFFER_LVALUE->yy_at_bol = (c == '\n'); 1475 | 1476 | return c; 1477 | } 1478 | #endif /* ifndef YY_NO_INPUT */ 1479 | 1480 | /** Immediately switch to a different input stream. 1481 | * @param input_file A readable stream. 1482 | * 1483 | * @note This function does not reset the start condition to @c INITIAL . 1484 | */ 1485 | void yyrestart (FILE * input_file ) 1486 | { 1487 | 1488 | if ( ! YY_CURRENT_BUFFER ){ 1489 | yyensure_buffer_stack (); 1490 | YY_CURRENT_BUFFER_LVALUE = 1491 | yy_create_buffer(yyin,YY_BUF_SIZE ); 1492 | } 1493 | 1494 | yy_init_buffer(YY_CURRENT_BUFFER,input_file ); 1495 | yy_load_buffer_state( ); 1496 | } 1497 | 1498 | /** Switch to a different input buffer. 1499 | * @param new_buffer The new input buffer. 1500 | * 1501 | */ 1502 | void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) 1503 | { 1504 | 1505 | /* TODO. We should be able to replace this entire function body 1506 | * with 1507 | * yypop_buffer_state(); 1508 | * yypush_buffer_state(new_buffer); 1509 | */ 1510 | yyensure_buffer_stack (); 1511 | if ( YY_CURRENT_BUFFER == new_buffer ) 1512 | return; 1513 | 1514 | if ( YY_CURRENT_BUFFER ) 1515 | { 1516 | /* Flush out information for old buffer. */ 1517 | *(yy_c_buf_p) = (yy_hold_char); 1518 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); 1519 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1520 | } 1521 | 1522 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1523 | yy_load_buffer_state( ); 1524 | 1525 | /* We don't actually know whether we did this switch during 1526 | * EOF (yywrap()) processing, but the only time this flag 1527 | * is looked at is after yywrap() is called, so it's safe 1528 | * to go ahead and always set it. 1529 | */ 1530 | (yy_did_buffer_switch_on_eof) = 1; 1531 | } 1532 | 1533 | static void yy_load_buffer_state (void) 1534 | { 1535 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 1536 | (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; 1537 | yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; 1538 | (yy_hold_char) = *(yy_c_buf_p); 1539 | } 1540 | 1541 | /** Allocate and initialize an input buffer state. 1542 | * @param file A readable stream. 1543 | * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. 1544 | * 1545 | * @return the allocated buffer state. 1546 | */ 1547 | YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) 1548 | { 1549 | YY_BUFFER_STATE b; 1550 | 1551 | b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); 1552 | if ( ! b ) 1553 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); 1554 | 1555 | b->yy_buf_size = size; 1556 | 1557 | /* yy_ch_buf has to be 2 characters longer than the size given because 1558 | * we need to put in 2 end-of-buffer characters. 1559 | */ 1560 | b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); 1561 | if ( ! b->yy_ch_buf ) 1562 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); 1563 | 1564 | b->yy_is_our_buffer = 1; 1565 | 1566 | yy_init_buffer(b,file ); 1567 | 1568 | return b; 1569 | } 1570 | 1571 | /** Destroy the buffer. 1572 | * @param b a buffer created with yy_create_buffer() 1573 | * 1574 | */ 1575 | void yy_delete_buffer (YY_BUFFER_STATE b ) 1576 | { 1577 | 1578 | if ( ! b ) 1579 | return; 1580 | 1581 | if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ 1582 | YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; 1583 | 1584 | if ( b->yy_is_our_buffer ) 1585 | yyfree((void *) b->yy_ch_buf ); 1586 | 1587 | yyfree((void *) b ); 1588 | } 1589 | 1590 | /* Initializes or reinitializes a buffer. 1591 | * This function is sometimes called more than once on the same buffer, 1592 | * such as during a yyrestart() or at EOF. 1593 | */ 1594 | static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) 1595 | 1596 | { 1597 | int oerrno = errno; 1598 | 1599 | yy_flush_buffer(b ); 1600 | 1601 | b->yy_input_file = file; 1602 | b->yy_fill_buffer = 1; 1603 | 1604 | /* If b is the current buffer, then yy_init_buffer was _probably_ 1605 | * called from yyrestart() or through yy_get_next_buffer. 1606 | * In that case, we don't want to reset the lineno or column. 1607 | */ 1608 | if (b != YY_CURRENT_BUFFER){ 1609 | b->yy_bs_lineno = 1; 1610 | b->yy_bs_column = 0; 1611 | } 1612 | 1613 | b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; 1614 | 1615 | errno = oerrno; 1616 | } 1617 | 1618 | /** Discard all buffered characters. On the next scan, YY_INPUT will be called. 1619 | * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. 1620 | * 1621 | */ 1622 | void yy_flush_buffer (YY_BUFFER_STATE b ) 1623 | { 1624 | if ( ! b ) 1625 | return; 1626 | 1627 | b->yy_n_chars = 0; 1628 | 1629 | /* We always need two end-of-buffer characters. The first causes 1630 | * a transition to the end-of-buffer state. The second causes 1631 | * a jam in that state. 1632 | */ 1633 | b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; 1634 | b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; 1635 | 1636 | b->yy_buf_pos = &b->yy_ch_buf[0]; 1637 | 1638 | b->yy_at_bol = 1; 1639 | b->yy_buffer_status = YY_BUFFER_NEW; 1640 | 1641 | if ( b == YY_CURRENT_BUFFER ) 1642 | yy_load_buffer_state( ); 1643 | } 1644 | 1645 | /** Pushes the new state onto the stack. The new state becomes 1646 | * the current state. This function will allocate the stack 1647 | * if necessary. 1648 | * @param new_buffer The new state. 1649 | * 1650 | */ 1651 | void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) 1652 | { 1653 | if (new_buffer == NULL) 1654 | return; 1655 | 1656 | yyensure_buffer_stack(); 1657 | 1658 | /* This block is copied from yy_switch_to_buffer. */ 1659 | if ( YY_CURRENT_BUFFER ) 1660 | { 1661 | /* Flush out information for old buffer. */ 1662 | *(yy_c_buf_p) = (yy_hold_char); 1663 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); 1664 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1665 | } 1666 | 1667 | /* Only push if top exists. Otherwise, replace top. */ 1668 | if (YY_CURRENT_BUFFER) 1669 | (yy_buffer_stack_top)++; 1670 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1671 | 1672 | /* copied from yy_switch_to_buffer. */ 1673 | yy_load_buffer_state( ); 1674 | (yy_did_buffer_switch_on_eof) = 1; 1675 | } 1676 | 1677 | /** Removes and deletes the top of the stack, if present. 1678 | * The next element becomes the new top. 1679 | * 1680 | */ 1681 | void yypop_buffer_state (void) 1682 | { 1683 | if (!YY_CURRENT_BUFFER) 1684 | return; 1685 | 1686 | yy_delete_buffer(YY_CURRENT_BUFFER ); 1687 | YY_CURRENT_BUFFER_LVALUE = NULL; 1688 | if ((yy_buffer_stack_top) > 0) 1689 | --(yy_buffer_stack_top); 1690 | 1691 | if (YY_CURRENT_BUFFER) { 1692 | yy_load_buffer_state( ); 1693 | (yy_did_buffer_switch_on_eof) = 1; 1694 | } 1695 | } 1696 | 1697 | /* Allocates the stack if it does not exist. 1698 | * Guarantees space for at least one push. 1699 | */ 1700 | static void yyensure_buffer_stack (void) 1701 | { 1702 | yy_size_t num_to_alloc; 1703 | 1704 | if (!(yy_buffer_stack)) { 1705 | 1706 | /* First allocation is just for 2 elements, since we don't know if this 1707 | * scanner will even need a stack. We use 2 instead of 1 to avoid an 1708 | * immediate realloc on the next call. 1709 | */ 1710 | num_to_alloc = 1; 1711 | (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc 1712 | (num_to_alloc * sizeof(struct yy_buffer_state*) 1713 | ); 1714 | if ( ! (yy_buffer_stack) ) 1715 | YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); 1716 | 1717 | memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); 1718 | 1719 | (yy_buffer_stack_max) = num_to_alloc; 1720 | (yy_buffer_stack_top) = 0; 1721 | return; 1722 | } 1723 | 1724 | if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ 1725 | 1726 | /* Increase the buffer to prepare for a possible push. */ 1727 | int grow_size = 8 /* arbitrary grow size */; 1728 | 1729 | num_to_alloc = (yy_buffer_stack_max) + grow_size; 1730 | (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc 1731 | ((yy_buffer_stack), 1732 | num_to_alloc * sizeof(struct yy_buffer_state*) 1733 | ); 1734 | if ( ! (yy_buffer_stack) ) 1735 | YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); 1736 | 1737 | /* zero only the new slots.*/ 1738 | memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); 1739 | (yy_buffer_stack_max) = num_to_alloc; 1740 | } 1741 | } 1742 | 1743 | /** Setup the input buffer state to scan directly from a user-specified character buffer. 1744 | * @param base the character buffer 1745 | * @param size the size in bytes of the character buffer 1746 | * 1747 | * @return the newly allocated buffer state object. 1748 | */ 1749 | YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) 1750 | { 1751 | YY_BUFFER_STATE b; 1752 | 1753 | if ( size < 2 || 1754 | base[size-2] != YY_END_OF_BUFFER_CHAR || 1755 | base[size-1] != YY_END_OF_BUFFER_CHAR ) 1756 | /* They forgot to leave room for the EOB's. */ 1757 | return 0; 1758 | 1759 | b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); 1760 | if ( ! b ) 1761 | YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); 1762 | 1763 | b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ 1764 | b->yy_buf_pos = b->yy_ch_buf = base; 1765 | b->yy_is_our_buffer = 0; 1766 | b->yy_input_file = 0; 1767 | b->yy_n_chars = b->yy_buf_size; 1768 | b->yy_is_interactive = 0; 1769 | b->yy_at_bol = 1; 1770 | b->yy_fill_buffer = 0; 1771 | b->yy_buffer_status = YY_BUFFER_NEW; 1772 | 1773 | yy_switch_to_buffer(b ); 1774 | 1775 | return b; 1776 | } 1777 | 1778 | /** Setup the input buffer state to scan a string. The next call to yylex() will 1779 | * scan from a @e copy of @a str. 1780 | * @param yystr a NUL-terminated string to scan 1781 | * 1782 | * @return the newly allocated buffer state object. 1783 | * @note If you want to scan bytes that may contain NUL values, then use 1784 | * yy_scan_bytes() instead. 1785 | */ 1786 | YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) 1787 | { 1788 | 1789 | return yy_scan_bytes(yystr,strlen(yystr) ); 1790 | } 1791 | 1792 | /** Setup the input buffer state to scan the given bytes. The next call to yylex() will 1793 | * scan from a @e copy of @a bytes. 1794 | * @param yybytes the byte buffer to scan 1795 | * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. 1796 | * 1797 | * @return the newly allocated buffer state object. 1798 | */ 1799 | YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len ) 1800 | { 1801 | YY_BUFFER_STATE b; 1802 | char *buf; 1803 | yy_size_t n; 1804 | yy_size_t i; 1805 | 1806 | /* Get memory for full buffer, including space for trailing EOB's. */ 1807 | n = _yybytes_len + 2; 1808 | buf = (char *) yyalloc(n ); 1809 | if ( ! buf ) 1810 | YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); 1811 | 1812 | for ( i = 0; i < _yybytes_len; ++i ) 1813 | buf[i] = yybytes[i]; 1814 | 1815 | buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; 1816 | 1817 | b = yy_scan_buffer(buf,n ); 1818 | if ( ! b ) 1819 | YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); 1820 | 1821 | /* It's okay to grow etc. this buffer, and we should throw it 1822 | * away when we're done. 1823 | */ 1824 | b->yy_is_our_buffer = 1; 1825 | 1826 | return b; 1827 | } 1828 | 1829 | #ifndef YY_EXIT_FAILURE 1830 | #define YY_EXIT_FAILURE 2 1831 | #endif 1832 | 1833 | static void yy_fatal_error (yyconst char* msg ) 1834 | { 1835 | (void) fprintf( stderr, "%s\n", msg ); 1836 | exit( YY_EXIT_FAILURE ); 1837 | } 1838 | 1839 | /* Redefine yyless() so it works in section 3 code. */ 1840 | 1841 | #undef yyless 1842 | #define yyless(n) \ 1843 | do \ 1844 | { \ 1845 | /* Undo effects of setting up yytext. */ \ 1846 | int yyless_macro_arg = (n); \ 1847 | YY_LESS_LINENO(yyless_macro_arg);\ 1848 | yytext[yyleng] = (yy_hold_char); \ 1849 | (yy_c_buf_p) = yytext + yyless_macro_arg; \ 1850 | (yy_hold_char) = *(yy_c_buf_p); \ 1851 | *(yy_c_buf_p) = '\0'; \ 1852 | yyleng = yyless_macro_arg; \ 1853 | } \ 1854 | while ( 0 ) 1855 | 1856 | /* Accessor methods (get/set functions) to struct members. */ 1857 | 1858 | /** Get the current line number. 1859 | * 1860 | */ 1861 | int yyget_lineno (void) 1862 | { 1863 | 1864 | return yylineno; 1865 | } 1866 | 1867 | /** Get the input stream. 1868 | * 1869 | */ 1870 | FILE *yyget_in (void) 1871 | { 1872 | return yyin; 1873 | } 1874 | 1875 | /** Get the output stream. 1876 | * 1877 | */ 1878 | FILE *yyget_out (void) 1879 | { 1880 | return yyout; 1881 | } 1882 | 1883 | /** Get the length of the current token. 1884 | * 1885 | */ 1886 | yy_size_t yyget_leng (void) 1887 | { 1888 | return yyleng; 1889 | } 1890 | 1891 | /** Get the current token. 1892 | * 1893 | */ 1894 | 1895 | char *yyget_text (void) 1896 | { 1897 | return yytext; 1898 | } 1899 | 1900 | /** Set the current line number. 1901 | * @param line_number 1902 | * 1903 | */ 1904 | void yyset_lineno (int line_number ) 1905 | { 1906 | 1907 | yylineno = line_number; 1908 | } 1909 | 1910 | /** Set the input stream. This does not discard the current 1911 | * input buffer. 1912 | * @param in_str A readable stream. 1913 | * 1914 | * @see yy_switch_to_buffer 1915 | */ 1916 | void yyset_in (FILE * in_str ) 1917 | { 1918 | yyin = in_str ; 1919 | } 1920 | 1921 | void yyset_out (FILE * out_str ) 1922 | { 1923 | yyout = out_str ; 1924 | } 1925 | 1926 | int yyget_debug (void) 1927 | { 1928 | return yy_flex_debug; 1929 | } 1930 | 1931 | void yyset_debug (int bdebug ) 1932 | { 1933 | yy_flex_debug = bdebug ; 1934 | } 1935 | 1936 | static int yy_init_globals (void) 1937 | { 1938 | /* Initialization is the same as for the non-reentrant scanner. 1939 | * This function is called from yylex_destroy(), so don't allocate here. 1940 | */ 1941 | 1942 | (yy_buffer_stack) = 0; 1943 | (yy_buffer_stack_top) = 0; 1944 | (yy_buffer_stack_max) = 0; 1945 | (yy_c_buf_p) = (char *) 0; 1946 | (yy_init) = 0; 1947 | (yy_start) = 0; 1948 | 1949 | /* Defined in main.c */ 1950 | #ifdef YY_STDINIT 1951 | yyin = stdin; 1952 | yyout = stdout; 1953 | #else 1954 | yyin = (FILE *) 0; 1955 | yyout = (FILE *) 0; 1956 | #endif 1957 | 1958 | /* For future reference: Set errno on error, since we are called by 1959 | * yylex_init() 1960 | */ 1961 | return 0; 1962 | } 1963 | 1964 | /* yylex_destroy is for both reentrant and non-reentrant scanners. */ 1965 | int yylex_destroy (void) 1966 | { 1967 | 1968 | /* Pop the buffer stack, destroying each element. */ 1969 | while(YY_CURRENT_BUFFER){ 1970 | yy_delete_buffer(YY_CURRENT_BUFFER ); 1971 | YY_CURRENT_BUFFER_LVALUE = NULL; 1972 | yypop_buffer_state(); 1973 | } 1974 | 1975 | /* Destroy the stack itself. */ 1976 | yyfree((yy_buffer_stack) ); 1977 | (yy_buffer_stack) = NULL; 1978 | 1979 | /* Reset the globals. This is important in a non-reentrant scanner so the next time 1980 | * yylex() is called, initialization will occur. */ 1981 | yy_init_globals( ); 1982 | 1983 | return 0; 1984 | } 1985 | 1986 | /* 1987 | * Internal utility routines. 1988 | */ 1989 | 1990 | #ifndef yytext_ptr 1991 | static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) 1992 | { 1993 | register int i; 1994 | for ( i = 0; i < n; ++i ) 1995 | s1[i] = s2[i]; 1996 | } 1997 | #endif 1998 | 1999 | #ifdef YY_NEED_STRLEN 2000 | static int yy_flex_strlen (yyconst char * s ) 2001 | { 2002 | register int n; 2003 | for ( n = 0; s[n]; ++n ) 2004 | ; 2005 | 2006 | return n; 2007 | } 2008 | #endif 2009 | 2010 | void *yyalloc (yy_size_t size ) 2011 | { 2012 | return (void *) malloc( size ); 2013 | } 2014 | 2015 | void *yyrealloc (void * ptr, yy_size_t size ) 2016 | { 2017 | /* The cast to (char *) in the following accommodates both 2018 | * implementations that use char* generic pointers, and those 2019 | * that use void* generic pointers. It works with the latter 2020 | * because both ANSI C and C++ allow castless assignment from 2021 | * any pointer type to void*, and deal with argument conversions 2022 | * as though doing an assignment. 2023 | */ 2024 | return (void *) realloc( (char *) ptr, size ); 2025 | } 2026 | 2027 | void yyfree (void * ptr ) 2028 | { 2029 | free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ 2030 | } 2031 | 2032 | #define YYTABLES_NAME "yytables" 2033 | 2034 | #line 55 "prog.l" 2035 | 2036 | 2037 | 2038 | 2039 | -------------------------------------------------------------------------------- /Semantic Analysis/out.txt: -------------------------------------------------------------------------------- 1 | ERROR: Undeclared Variable c 2 | ERROR: Void Type Error 3 | -------------------------------------------------------------------------------- /Semantic Analysis/output.txt: -------------------------------------------------------------------------------- 1 | Abstract Syntax Tree 2 | ( * 3 4 ) 3 | ( * 5 ( + 6 7 )) 4 | ( + 4 ( + ( * 3 4 )( * 5 ( / 8 9 )))) 5 | 'd' 6 | -------------------------------------------------------------------------------- /Semantic Analysis/prog.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | #include 6 | %} 7 | 8 | alpha [a-zA-Z] 9 | digit [0-9] 10 | exp (E|e){digit}+ 11 | 12 | %% 13 | "-" return SUB; 14 | "+" return ADD; 15 | "*" return MUL; 16 | "/" return DIV; 17 | ">=" return GE; 18 | "<=" return LE; 19 | "==" return EQ; 20 | "<" return L; 21 | ">" return G; 22 | "!=" return NEQ; 23 | "=" {return ASSIGN;} 24 | "&&" return AND; 25 | "||" return OR; 26 | "++" return INC; 27 | "--" return DEC; 28 | "^" return XOR; 29 | "switch" return SWITCH; 30 | "if" return IF; 31 | "else" return ELSE; 32 | "break" return BREAK; 33 | "char" return CHAR; 34 | "int" return INT; 35 | "while" return WHILE; 36 | "float" return FLOAT; 37 | "default" return DEFAULT; 38 | "case" return CASE; 39 | "void" return VOID; 40 | "return" return RETURN; 41 | "double" return DOUBLE; 42 | '.' {yylval=(char*)yytext;return CHARVALUE;} 43 | {alpha}({alpha}|{digit})* {yylval=(char*)yytext;return IDENTIFIER;} 44 | {digit}+ {yylval=(char*)yytext;return NUM;} 45 | {digit}*\.{digit}+{exp}? {yylval=(char*)yytext;return REAL;} 46 | {digit}+\.{digit}*{exp}? {yylval=(char*)yytext;return REAL;} 47 | ";" return SEMICOLON; 48 | "," return COMMA; 49 | "(" return OP; 50 | ")" return CP; 51 | "{" return OB; 52 | "}" return CB; 53 | ^#.* {yylval=(char*)yytext;return HEADER;} 54 | [ \t\n] ; 55 | . return yytext; 56 | %% 57 | 58 | -------------------------------------------------------------------------------- /Semantic Analysis/prog.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | void typecheck(int,int); 6 | int typeassign(int,int); 7 | int declarationcheck(char a[20],int); 8 | int variablecheck(char a[20]); 9 | int countentries=0; 10 | struct table 11 | { 12 | char name[20]; 13 | int type; 14 | }arr[100]; 15 | char temp[20]; 16 | int typeno; 17 | FILE* yyout; 18 | %} 19 | 20 | %token IDENTIFIER NUM HEADER REAL CHARVALUE 21 | %token CHAR INT FLOAT DOUBLE VOID RETURN 22 | %token EQ LE GE AND OR XOR ASSIGN L G NEQ 23 | %token IF ELSE SWITCH BREAK WHILE CASE DEFAULT 24 | %token ADD SUB MUL DIV INC DEC 25 | %token SEMICOLON COMMA 26 | %token OP CP OB CB 27 | 28 | %start program 29 | 30 | %% 31 | program 32 | : header programnext {printf("Input accepted\n");exit(0);} 33 | ; 34 | header 35 | : HEADER 36 | | HEADER header 37 | ; 38 | programnext 39 | : declarations 40 | | function 41 | | declarations function 42 | | function function 43 | ; 44 | returnstatement 45 | : RETURN expressions SEMICOLON 46 | | {;} 47 | ; 48 | declarations 49 | : type assignmentlist SEMICOLON 50 | ; 51 | assignmentlist 52 | : variable ASSIGN number 53 | | variable COMMA assignmentlist 54 | | variable 55 | ; 56 | number 57 | : NUM 58 | | REAL 59 | | CHARVALUE 60 | ; 61 | type 62 | : INT {$$=2;typeno=2;} 63 | | CHAR {$$=1;typeno=1;} 64 | | FLOAT {$$=3;typeno=3;} 65 | | VOID {$$=0;typeno=0;} 66 | ; 67 | function 68 | : type IDENTIFIER OP argumentlist CP OB statements returnstatement CB 69 | | function function 70 | | {;} 71 | ; 72 | 73 | variable 74 | : IDENTIFIER {strcpy(temp,$1);$$=declarationcheck(temp,typeno);} 75 | ; 76 | argument 77 | : type variable 78 | ; 79 | argumentlist 80 | : argument 81 | | argument COMMA argumentlist 82 | | {;} 83 | ; 84 | id 85 | : IDENTIFIER {strcpy(temp,$1);$$=variablecheck(temp);} 86 | ; 87 | statements 88 | : id ASSIGN expressions SEMICOLON {typecheck($1,$3);} 89 | | type variable ASSIGN expressions SEMICOLON {typecheck($1,$4);} 90 | | ifstatements 91 | | switchstatements 92 | | whilestatements 93 | | statements statements 94 | ; 95 | ifstatements 96 | : IF OP conditionalexpressions CP OB statements CB ELSE OB statements CB 97 | ; 98 | switchstatements 99 | : SWITCH OP id CP OB casestatements CB 100 | ; 101 | whilestatements 102 | : WHILE OP conditionalexpressions CP OB statements CB 103 | ; 104 | casestatements 105 | : CASE NUM ':' statements BREAK SEMICOLON casestatements 106 | | DEFAULT ':' statements BREAK SEMICOLON 107 | | {;} 108 | ; 109 | expressions 110 | : conditionalexpressions {$$=$1;} 111 | | expressions ADD expressions {$$=typeassign($1,$3);} 112 | | expressions SUB expressions {$$=typeassign($1,$3);} 113 | | expressions MUL expressions {$$=typeassign($1,$3);} 114 | | expressions DIV expressions {$$=typeassign($1,$3);} 115 | | IDENTIFIER {strcpy(temp,$1);$$=variablecheck(temp);} 116 | | NUM {$$=2;} 117 | | OP expressions CP {$$=$2;} 118 | | INC IDENTIFIER {strcpy(temp,$2);$$=variablecheck(temp);if($$!=2) fprintf(yyout,"ERROR: Increment Decrement on only Integers\n");} 119 | | DEC IDENTIFIER {strcpy(temp,$2);$$=variablecheck(temp);if($$!=2) fprintf(yyout,"ERROR: Increment Decrement on only Integers\n");} 120 | | REAL {$$=3;} 121 | | CHARVALUE {$$=1;} 122 | ; 123 | conditionalexpressions 124 | : expressions AND expressions {$$=2;} 125 | | expressions OR expressions {$$=2;} 126 | | expressions LE expressions {$$=2;} 127 | | expressions L expressions {$$=2;} 128 | | expressions G expressions {$$=2;} 129 | | expressions NEQ expressions {$$=2;} 130 | | expressions GE expressions {$$=2;} 131 | | expressions EQ expressions {$$=2;} 132 | | OP conditionalexpressions CP {$$=$2;} 133 | ; 134 | %% 135 | 136 | void typecheck(int a,int b) 137 | { 138 | if(a==0 || b==0) 139 | { 140 | fprintf(yyout,"ERROR: Void Type Error\n"); 141 | return 0; 142 | } 143 | if(a!=b) 144 | { 145 | fprintf(yyout,"ERROR: Type Mismatch\n"); 146 | if(a==1 && b==2) 147 | fprintf(yyout,"Note: Implicit conversion of Int type to Char\n"); 148 | else if(a==2 && b==3) 149 | fprintf(yyout,"Note: Implicit conversion of Float type expression to Int\n"); 150 | else if(a==3) 151 | fprintf(yyout,"Note: Implicit conversion to Float Type\n"); 152 | } 153 | } 154 | int typeassign(int a,int b) 155 | { 156 | if(a==0 || b==0) 157 | return 0; 158 | else if(a==b) 159 | return a; 160 | else if((a==1 && b==2)||(b==1&&a==2)) 161 | return 2; 162 | else if((a==2 && b==3)||(a==3 && b==2)) 163 | return 3; 164 | else return 3; 165 | } 166 | int declarationcheck(char a[20],int type) 167 | { 168 | int j=0; 169 | for(j=0;j 0 13 | #define FLEX_BETA 14 | #endif 15 | 16 | /* First, we deal with platform-specific or compiler-specific issues. */ 17 | 18 | /* begin standard C headers. */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | /* end standard C headers. */ 25 | 26 | /* flex integer type definitions */ 27 | 28 | #ifndef FLEXINT_H 29 | #define FLEXINT_H 30 | 31 | /* C99 systems have . Non-C99 systems may or may not. */ 32 | 33 | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 34 | 35 | /* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, 36 | * if you want the limit (max/min) macros for int types. 37 | */ 38 | #ifndef __STDC_LIMIT_MACROS 39 | #define __STDC_LIMIT_MACROS 1 40 | #endif 41 | 42 | #include 43 | typedef int8_t flex_int8_t; 44 | typedef uint8_t flex_uint8_t; 45 | typedef int16_t flex_int16_t; 46 | typedef uint16_t flex_uint16_t; 47 | typedef int32_t flex_int32_t; 48 | typedef uint32_t flex_uint32_t; 49 | #else 50 | typedef signed char flex_int8_t; 51 | typedef short int flex_int16_t; 52 | typedef int flex_int32_t; 53 | typedef unsigned char flex_uint8_t; 54 | typedef unsigned short int flex_uint16_t; 55 | typedef unsigned int flex_uint32_t; 56 | 57 | /* Limits of integral types. */ 58 | #ifndef INT8_MIN 59 | #define INT8_MIN (-128) 60 | #endif 61 | #ifndef INT16_MIN 62 | #define INT16_MIN (-32767-1) 63 | #endif 64 | #ifndef INT32_MIN 65 | #define INT32_MIN (-2147483647-1) 66 | #endif 67 | #ifndef INT8_MAX 68 | #define INT8_MAX (127) 69 | #endif 70 | #ifndef INT16_MAX 71 | #define INT16_MAX (32767) 72 | #endif 73 | #ifndef INT32_MAX 74 | #define INT32_MAX (2147483647) 75 | #endif 76 | #ifndef UINT8_MAX 77 | #define UINT8_MAX (255U) 78 | #endif 79 | #ifndef UINT16_MAX 80 | #define UINT16_MAX (65535U) 81 | #endif 82 | #ifndef UINT32_MAX 83 | #define UINT32_MAX (4294967295U) 84 | #endif 85 | 86 | #endif /* ! C99 */ 87 | 88 | #endif /* ! FLEXINT_H */ 89 | 90 | #ifdef __cplusplus 91 | 92 | /* The "const" storage-class-modifier is valid. */ 93 | #define YY_USE_CONST 94 | 95 | #else /* ! __cplusplus */ 96 | 97 | /* C99 requires __STDC__ to be defined as 1. */ 98 | #if defined (__STDC__) 99 | 100 | #define YY_USE_CONST 101 | 102 | #endif /* defined (__STDC__) */ 103 | #endif /* ! __cplusplus */ 104 | 105 | #ifdef YY_USE_CONST 106 | #define yyconst const 107 | #else 108 | #define yyconst 109 | #endif 110 | 111 | /* Returned upon end-of-file. */ 112 | #define YY_NULL 0 113 | 114 | /* Promotes a possibly negative, possibly signed char to an unsigned 115 | * integer for use as an array index. If the signed char is negative, 116 | * we want to instead treat it as an 8-bit unsigned char, hence the 117 | * double cast. 118 | */ 119 | #define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) 120 | 121 | /* Enter a start condition. This macro really ought to take a parameter, 122 | * but we do it the disgusting crufty way forced on us by the ()-less 123 | * definition of BEGIN. 124 | */ 125 | #define BEGIN (yy_start) = 1 + 2 * 126 | 127 | /* Translate the current start state into a value that can be later handed 128 | * to BEGIN to return to the state. The YYSTATE alias is for lex 129 | * compatibility. 130 | */ 131 | #define YY_START (((yy_start) - 1) / 2) 132 | #define YYSTATE YY_START 133 | 134 | /* Action number for EOF rule of a given start state. */ 135 | #define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) 136 | 137 | /* Special action meaning "start processing a new file". */ 138 | #define YY_NEW_FILE yyrestart(yyin ) 139 | 140 | #define YY_END_OF_BUFFER_CHAR 0 141 | 142 | /* Size of default input buffer. */ 143 | #ifndef YY_BUF_SIZE 144 | #ifdef __ia64__ 145 | /* On IA-64, the buffer size is 16k, not 8k. 146 | * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. 147 | * Ditto for the __ia64__ case accordingly. 148 | */ 149 | #define YY_BUF_SIZE 32768 150 | #else 151 | #define YY_BUF_SIZE 16384 152 | #endif /* __ia64__ */ 153 | #endif 154 | 155 | /* The state buf must be large enough to hold one state per character in the main buffer. 156 | */ 157 | #define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) 158 | 159 | #ifndef YY_TYPEDEF_YY_BUFFER_STATE 160 | #define YY_TYPEDEF_YY_BUFFER_STATE 161 | typedef struct yy_buffer_state *YY_BUFFER_STATE; 162 | #endif 163 | 164 | #ifndef YY_TYPEDEF_YY_SIZE_T 165 | #define YY_TYPEDEF_YY_SIZE_T 166 | typedef size_t yy_size_t; 167 | #endif 168 | 169 | extern yy_size_t yyleng; 170 | 171 | extern FILE *yyin, *yyout; 172 | 173 | #define EOB_ACT_CONTINUE_SCAN 0 174 | #define EOB_ACT_END_OF_FILE 1 175 | #define EOB_ACT_LAST_MATCH 2 176 | 177 | #define YY_LESS_LINENO(n) 178 | #define YY_LINENO_REWIND_TO(ptr) 179 | 180 | /* Return all but the first "n" matched characters back to the input stream. */ 181 | #define yyless(n) \ 182 | do \ 183 | { \ 184 | /* Undo effects of setting up yytext. */ \ 185 | int yyless_macro_arg = (n); \ 186 | YY_LESS_LINENO(yyless_macro_arg);\ 187 | *yy_cp = (yy_hold_char); \ 188 | YY_RESTORE_YY_MORE_OFFSET \ 189 | (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ 190 | YY_DO_BEFORE_ACTION; /* set up yytext again */ \ 191 | } \ 192 | while ( 0 ) 193 | 194 | #define unput(c) yyunput( c, (yytext_ptr) ) 195 | 196 | #ifndef YY_STRUCT_YY_BUFFER_STATE 197 | #define YY_STRUCT_YY_BUFFER_STATE 198 | struct yy_buffer_state 199 | { 200 | FILE *yy_input_file; 201 | 202 | char *yy_ch_buf; /* input buffer */ 203 | char *yy_buf_pos; /* current position in input buffer */ 204 | 205 | /* Size of input buffer in bytes, not including room for EOB 206 | * characters. 207 | */ 208 | yy_size_t yy_buf_size; 209 | 210 | /* Number of characters read into yy_ch_buf, not including EOB 211 | * characters. 212 | */ 213 | yy_size_t yy_n_chars; 214 | 215 | /* Whether we "own" the buffer - i.e., we know we created it, 216 | * and can realloc() it to grow it, and should free() it to 217 | * delete it. 218 | */ 219 | int yy_is_our_buffer; 220 | 221 | /* Whether this is an "interactive" input source; if so, and 222 | * if we're using stdio for input, then we want to use getc() 223 | * instead of fread(), to make sure we stop fetching input after 224 | * each newline. 225 | */ 226 | int yy_is_interactive; 227 | 228 | /* Whether we're considered to be at the beginning of a line. 229 | * If so, '^' rules will be active on the next match, otherwise 230 | * not. 231 | */ 232 | int yy_at_bol; 233 | 234 | int yy_bs_lineno; /**< The line count. */ 235 | int yy_bs_column; /**< The column count. */ 236 | 237 | /* Whether to try to fill the input buffer when we reach the 238 | * end of it. 239 | */ 240 | int yy_fill_buffer; 241 | 242 | int yy_buffer_status; 243 | 244 | #define YY_BUFFER_NEW 0 245 | #define YY_BUFFER_NORMAL 1 246 | /* When an EOF's been seen but there's still some text to process 247 | * then we mark the buffer as YY_EOF_PENDING, to indicate that we 248 | * shouldn't try reading from the input source any more. We might 249 | * still have a bunch of tokens to match, though, because of 250 | * possible backing-up. 251 | * 252 | * When we actually see the EOF, we change the status to "new" 253 | * (via yyrestart()), so that the user can continue scanning by 254 | * just pointing yyin at a new input file. 255 | */ 256 | #define YY_BUFFER_EOF_PENDING 2 257 | 258 | }; 259 | #endif /* !YY_STRUCT_YY_BUFFER_STATE */ 260 | 261 | /* Stack of input buffers. */ 262 | static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ 263 | static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ 264 | static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ 265 | 266 | /* We provide macros for accessing buffer states in case in the 267 | * future we want to put the buffer states in a more general 268 | * "scanner state". 269 | * 270 | * Returns the top of the stack, or NULL. 271 | */ 272 | #define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ 273 | ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ 274 | : NULL) 275 | 276 | /* Same as previous macro, but useful when we know that the buffer stack is not 277 | * NULL or when we need an lvalue. For internal use only. 278 | */ 279 | #define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] 280 | 281 | /* yy_hold_char holds the character lost when yytext is formed. */ 282 | static char yy_hold_char; 283 | static yy_size_t yy_n_chars; /* number of characters read into yy_ch_buf */ 284 | yy_size_t yyleng; 285 | 286 | /* Points to current character in buffer. */ 287 | static char *yy_c_buf_p = (char *) 0; 288 | static int yy_init = 0; /* whether we need to initialize */ 289 | static int yy_start = 0; /* start state number */ 290 | 291 | /* Flag which is used to allow yywrap()'s to do buffer switches 292 | * instead of setting up a fresh yyin. A bit of a hack ... 293 | */ 294 | static int yy_did_buffer_switch_on_eof; 295 | 296 | void yyrestart (FILE *input_file ); 297 | void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); 298 | YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); 299 | void yy_delete_buffer (YY_BUFFER_STATE b ); 300 | void yy_flush_buffer (YY_BUFFER_STATE b ); 301 | void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); 302 | void yypop_buffer_state (void ); 303 | 304 | static void yyensure_buffer_stack (void ); 305 | static void yy_load_buffer_state (void ); 306 | static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); 307 | 308 | #define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) 309 | 310 | YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); 311 | YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); 312 | YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); 313 | 314 | void *yyalloc (yy_size_t ); 315 | void *yyrealloc (void *,yy_size_t ); 316 | void yyfree (void * ); 317 | 318 | #define yy_new_buffer yy_create_buffer 319 | 320 | #define yy_set_interactive(is_interactive) \ 321 | { \ 322 | if ( ! YY_CURRENT_BUFFER ){ \ 323 | yyensure_buffer_stack (); \ 324 | YY_CURRENT_BUFFER_LVALUE = \ 325 | yy_create_buffer(yyin,YY_BUF_SIZE ); \ 326 | } \ 327 | YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ 328 | } 329 | 330 | #define yy_set_bol(at_bol) \ 331 | { \ 332 | if ( ! YY_CURRENT_BUFFER ){\ 333 | yyensure_buffer_stack (); \ 334 | YY_CURRENT_BUFFER_LVALUE = \ 335 | yy_create_buffer(yyin,YY_BUF_SIZE ); \ 336 | } \ 337 | YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ 338 | } 339 | 340 | #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) 341 | 342 | /* Begin user sect3 */ 343 | 344 | typedef unsigned char YY_CHAR; 345 | 346 | FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; 347 | 348 | typedef int yy_state_type; 349 | 350 | extern int yylineno; 351 | 352 | int yylineno = 1; 353 | 354 | extern char *yytext; 355 | #define yytext_ptr yytext 356 | 357 | static yy_state_type yy_get_previous_state (void ); 358 | static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); 359 | static int yy_get_next_buffer (void ); 360 | static void yy_fatal_error (yyconst char msg[] ); 361 | 362 | /* Done after the current pattern has been matched and before the 363 | * corresponding action - sets up yytext. 364 | */ 365 | #define YY_DO_BEFORE_ACTION \ 366 | (yytext_ptr) = yy_bp; \ 367 | yyleng = (size_t) (yy_cp - yy_bp); \ 368 | (yy_hold_char) = *yy_cp; \ 369 | *yy_cp = '\0'; \ 370 | (yy_c_buf_p) = yy_cp; 371 | 372 | #define YY_NUM_RULES 32 373 | #define YY_END_OF_BUFFER 33 374 | /* This struct is not used in this scanner, 375 | but its presence is necessary. */ 376 | struct yy_trans_info 377 | { 378 | flex_int32_t yy_verify; 379 | flex_int32_t yy_nxt; 380 | }; 381 | static yyconst flex_int16_t yy_accept[75] = 382 | { 0, 383 | 0, 0, 33, 30, 31, 30, 30, 26, 27, 3, 384 | 2, 25, 1, 4, 23, 24, 30, 8, 30, 22, 385 | 11, 22, 22, 22, 22, 22, 22, 22, 22, 28, 386 | 30, 29, 9, 23, 6, 7, 5, 22, 22, 22, 387 | 22, 22, 22, 22, 13, 22, 22, 22, 10, 22, 388 | 22, 22, 22, 22, 22, 17, 22, 22, 22, 20, 389 | 16, 22, 14, 22, 22, 22, 15, 22, 19, 22, 390 | 18, 21, 12, 0 391 | } ; 392 | 393 | static yyconst flex_int32_t yy_ec[256] = 394 | { 0, 395 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 396 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 397 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 398 | 1, 3, 1, 1, 1, 1, 1, 4, 1, 5, 399 | 6, 7, 8, 9, 10, 1, 11, 12, 12, 12, 400 | 12, 12, 12, 12, 12, 12, 12, 1, 13, 14, 401 | 15, 16, 1, 1, 17, 17, 17, 17, 17, 17, 402 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 403 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 404 | 1, 1, 1, 18, 1, 1, 19, 20, 21, 22, 405 | 406 | 23, 24, 17, 25, 26, 17, 27, 28, 17, 29, 407 | 30, 17, 17, 31, 32, 33, 34, 17, 35, 17, 408 | 17, 17, 36, 37, 38, 1, 1, 1, 1, 1, 409 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 410 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 411 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 412 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 413 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 414 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 415 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 416 | 417 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 418 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 419 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 420 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 421 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 422 | 1, 1, 1, 1, 1 423 | } ; 424 | 425 | static yyconst flex_int32_t yy_meta[39] = 426 | { 0, 427 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 428 | 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 429 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 430 | 2, 2, 2, 2, 2, 1, 1, 1 431 | } ; 432 | 433 | static yyconst flex_int16_t yy_base[76] = 434 | { 0, 435 | 0, 0, 81, 82, 82, 82, 76, 82, 82, 82, 436 | 82, 82, 82, 82, 67, 82, 63, 62, 61, 0, 437 | 82, 44, 20, 44, 45, 44, 17, 36, 45, 82, 438 | 32, 82, 82, 56, 82, 82, 82, 0, 44, 34, 439 | 46, 30, 31, 32, 0, 28, 34, 33, 82, 39, 440 | 34, 25, 35, 31, 34, 0, 19, 23, 23, 0, 441 | 0, 21, 0, 15, 26, 21, 0, 20, 0, 17, 442 | 0, 0, 0, 82, 38 443 | } ; 444 | 445 | static yyconst flex_int16_t yy_def[76] = 446 | { 0, 447 | 74, 1, 74, 74, 74, 74, 74, 74, 74, 74, 448 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 449 | 74, 75, 75, 75, 75, 75, 75, 75, 75, 74, 450 | 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 451 | 75, 75, 75, 75, 75, 75, 75, 75, 74, 75, 452 | 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 453 | 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 454 | 75, 75, 75, 0, 74 455 | } ; 456 | 457 | static yyconst flex_int16_t yy_nxt[121] = 458 | { 0, 459 | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 460 | 14, 15, 16, 17, 18, 19, 20, 21, 20, 22, 461 | 23, 24, 25, 26, 20, 27, 20, 20, 20, 20, 462 | 20, 28, 20, 20, 29, 30, 31, 32, 40, 38, 463 | 45, 73, 72, 71, 41, 46, 70, 69, 68, 67, 464 | 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 465 | 56, 55, 54, 53, 52, 51, 50, 34, 49, 48, 466 | 47, 44, 43, 42, 39, 37, 36, 35, 34, 33, 467 | 74, 3, 74, 74, 74, 74, 74, 74, 74, 74, 468 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 469 | 470 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 471 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74 472 | } ; 473 | 474 | static yyconst flex_int16_t yy_chk[121] = 475 | { 0, 476 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 477 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 478 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 479 | 1, 1, 1, 1, 1, 1, 1, 1, 23, 75, 480 | 27, 70, 68, 66, 23, 27, 65, 64, 62, 59, 481 | 58, 57, 55, 54, 53, 52, 51, 50, 48, 47, 482 | 46, 44, 43, 42, 41, 40, 39, 34, 31, 29, 483 | 28, 26, 25, 24, 22, 19, 18, 17, 15, 7, 484 | 3, 74, 74, 74, 74, 74, 74, 74, 74, 74, 485 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 486 | 487 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 488 | 74, 74, 74, 74, 74, 74, 74, 74, 74, 74 489 | } ; 490 | 491 | static yy_state_type yy_last_accepting_state; 492 | static char *yy_last_accepting_cpos; 493 | 494 | extern int yy_flex_debug; 495 | int yy_flex_debug = 0; 496 | 497 | /* The intent behind this definition is that it'll catch 498 | * any uses of REJECT which flex missed. 499 | */ 500 | #define REJECT reject_used_but_not_detected 501 | #define yymore() yymore_used_but_not_detected 502 | #define YY_MORE_ADJ 0 503 | #define YY_RESTORE_YY_MORE_OFFSET 504 | char *yytext; 505 | #line 1 "prog.l" 506 | #line 2 "prog.l" 507 | #include 508 | #include 509 | #include 510 | #include 511 | #line 512 "lex.yy.c" 512 | 513 | #define INITIAL 0 514 | 515 | #ifndef YY_NO_UNISTD_H 516 | /* Special case for "unistd.h", since it is non-ANSI. We include it way 517 | * down here because we want the user's section 1 to have been scanned first. 518 | * The user has a chance to override it with an option. 519 | */ 520 | #include 521 | #endif 522 | 523 | #ifndef YY_EXTRA_TYPE 524 | #define YY_EXTRA_TYPE void * 525 | #endif 526 | 527 | static int yy_init_globals (void ); 528 | 529 | /* Accessor methods to globals. 530 | These are made visible to non-reentrant scanners for convenience. */ 531 | 532 | int yylex_destroy (void ); 533 | 534 | int yyget_debug (void ); 535 | 536 | void yyset_debug (int debug_flag ); 537 | 538 | YY_EXTRA_TYPE yyget_extra (void ); 539 | 540 | void yyset_extra (YY_EXTRA_TYPE user_defined ); 541 | 542 | FILE *yyget_in (void ); 543 | 544 | void yyset_in (FILE * in_str ); 545 | 546 | FILE *yyget_out (void ); 547 | 548 | void yyset_out (FILE * out_str ); 549 | 550 | yy_size_t yyget_leng (void ); 551 | 552 | char *yyget_text (void ); 553 | 554 | int yyget_lineno (void ); 555 | 556 | void yyset_lineno (int line_number ); 557 | 558 | /* Macros after this point can all be overridden by user definitions in 559 | * section 1. 560 | */ 561 | 562 | #ifndef YY_SKIP_YYWRAP 563 | #ifdef __cplusplus 564 | extern "C" int yywrap (void ); 565 | #else 566 | extern int yywrap (void ); 567 | #endif 568 | #endif 569 | 570 | static void yyunput (int c,char *buf_ptr ); 571 | 572 | #ifndef yytext_ptr 573 | static void yy_flex_strncpy (char *,yyconst char *,int ); 574 | #endif 575 | 576 | #ifdef YY_NEED_STRLEN 577 | static int yy_flex_strlen (yyconst char * ); 578 | #endif 579 | 580 | #ifndef YY_NO_INPUT 581 | 582 | #ifdef __cplusplus 583 | static int yyinput (void ); 584 | #else 585 | static int input (void ); 586 | #endif 587 | 588 | #endif 589 | 590 | /* Amount of stuff to slurp up with each read. */ 591 | #ifndef YY_READ_BUF_SIZE 592 | #ifdef __ia64__ 593 | /* On IA-64, the buffer size is 16k, not 8k */ 594 | #define YY_READ_BUF_SIZE 16384 595 | #else 596 | #define YY_READ_BUF_SIZE 8192 597 | #endif /* __ia64__ */ 598 | #endif 599 | 600 | /* Copy whatever the last rule matched to the standard output. */ 601 | #ifndef ECHO 602 | /* This used to be an fputs(), but since the string might contain NUL's, 603 | * we now use fwrite(). 604 | */ 605 | #define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0) 606 | #endif 607 | 608 | /* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, 609 | * is returned in "result". 610 | */ 611 | #ifndef YY_INPUT 612 | #define YY_INPUT(buf,result,max_size) \ 613 | if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ 614 | { \ 615 | int c = '*'; \ 616 | size_t n; \ 617 | for ( n = 0; n < max_size && \ 618 | (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ 619 | buf[n] = (char) c; \ 620 | if ( c == '\n' ) \ 621 | buf[n++] = (char) c; \ 622 | if ( c == EOF && ferror( yyin ) ) \ 623 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 624 | result = n; \ 625 | } \ 626 | else \ 627 | { \ 628 | errno=0; \ 629 | while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ 630 | { \ 631 | if( errno != EINTR) \ 632 | { \ 633 | YY_FATAL_ERROR( "input in flex scanner failed" ); \ 634 | break; \ 635 | } \ 636 | errno=0; \ 637 | clearerr(yyin); \ 638 | } \ 639 | }\ 640 | \ 641 | 642 | #endif 643 | 644 | /* No semi-colon after return; correct usage is to write "yyterminate();" - 645 | * we don't want an extra ';' after the "return" because that will cause 646 | * some compilers to complain about unreachable statements. 647 | */ 648 | #ifndef yyterminate 649 | #define yyterminate() return YY_NULL 650 | #endif 651 | 652 | /* Number of entries by which start-condition stack grows. */ 653 | #ifndef YY_START_STACK_INCR 654 | #define YY_START_STACK_INCR 25 655 | #endif 656 | 657 | /* Report a fatal error. */ 658 | #ifndef YY_FATAL_ERROR 659 | #define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) 660 | #endif 661 | 662 | /* end tables serialization structures and prototypes */ 663 | 664 | /* Default declaration of generated scanner - a define so the user can 665 | * easily add parameters. 666 | */ 667 | #ifndef YY_DECL 668 | #define YY_DECL_IS_OURS 1 669 | 670 | extern int yylex (void); 671 | 672 | #define YY_DECL int yylex (void) 673 | #endif /* !YY_DECL */ 674 | 675 | /* Code executed at the beginning of each rule, after yytext and yyleng 676 | * have been set up. 677 | */ 678 | #ifndef YY_USER_ACTION 679 | #define YY_USER_ACTION 680 | #endif 681 | 682 | /* Code executed at the end of each rule. */ 683 | #ifndef YY_BREAK 684 | #define YY_BREAK break; 685 | #endif 686 | 687 | #define YY_RULE_SETUP \ 688 | YY_USER_ACTION 689 | 690 | /** The main scanner function which does all the work. 691 | */ 692 | YY_DECL 693 | { 694 | register yy_state_type yy_current_state; 695 | register char *yy_cp, *yy_bp; 696 | register int yy_act; 697 | 698 | if ( !(yy_init) ) 699 | { 700 | (yy_init) = 1; 701 | 702 | #ifdef YY_USER_INIT 703 | YY_USER_INIT; 704 | #endif 705 | 706 | if ( ! (yy_start) ) 707 | (yy_start) = 1; /* first start state */ 708 | 709 | if ( ! yyin ) 710 | yyin = stdin; 711 | 712 | if ( ! yyout ) 713 | yyout = stdout; 714 | 715 | if ( ! YY_CURRENT_BUFFER ) { 716 | yyensure_buffer_stack (); 717 | YY_CURRENT_BUFFER_LVALUE = 718 | yy_create_buffer(yyin,YY_BUF_SIZE ); 719 | } 720 | 721 | yy_load_buffer_state( ); 722 | } 723 | 724 | { 725 | #line 11 "prog.l" 726 | 727 | #line 728 "lex.yy.c" 728 | 729 | while ( 1 ) /* loops until end-of-file is reached */ 730 | { 731 | yy_cp = (yy_c_buf_p); 732 | 733 | /* Support of yytext. */ 734 | *yy_cp = (yy_hold_char); 735 | 736 | /* yy_bp points to the position in yy_ch_buf of the start of 737 | * the current run. 738 | */ 739 | yy_bp = yy_cp; 740 | 741 | yy_current_state = (yy_start); 742 | yy_match: 743 | do 744 | { 745 | register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; 746 | if ( yy_accept[yy_current_state] ) 747 | { 748 | (yy_last_accepting_state) = yy_current_state; 749 | (yy_last_accepting_cpos) = yy_cp; 750 | } 751 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 752 | { 753 | yy_current_state = (int) yy_def[yy_current_state]; 754 | if ( yy_current_state >= 75 ) 755 | yy_c = yy_meta[(unsigned int) yy_c]; 756 | } 757 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 758 | ++yy_cp; 759 | } 760 | while ( yy_base[yy_current_state] != 82 ); 761 | 762 | yy_find_action: 763 | yy_act = yy_accept[yy_current_state]; 764 | if ( yy_act == 0 ) 765 | { /* have to back up */ 766 | yy_cp = (yy_last_accepting_cpos); 767 | yy_current_state = (yy_last_accepting_state); 768 | yy_act = yy_accept[yy_current_state]; 769 | } 770 | 771 | YY_DO_BEFORE_ACTION; 772 | 773 | do_action: /* This label is used only to access EOF actions. */ 774 | 775 | switch ( yy_act ) 776 | { /* beginning of action switch */ 777 | case 0: /* must back up */ 778 | /* undo the effects of YY_DO_BEFORE_ACTION */ 779 | *yy_cp = (yy_hold_char); 780 | yy_cp = (yy_last_accepting_cpos); 781 | yy_current_state = (yy_last_accepting_state); 782 | goto yy_find_action; 783 | 784 | case 1: 785 | YY_RULE_SETUP 786 | #line 12 "prog.l" 787 | return SUB; 788 | YY_BREAK 789 | case 2: 790 | YY_RULE_SETUP 791 | #line 13 "prog.l" 792 | return ADD; 793 | YY_BREAK 794 | case 3: 795 | YY_RULE_SETUP 796 | #line 14 "prog.l" 797 | return MUL; 798 | YY_BREAK 799 | case 4: 800 | YY_RULE_SETUP 801 | #line 15 "prog.l" 802 | return DIV; 803 | YY_BREAK 804 | case 5: 805 | YY_RULE_SETUP 806 | #line 16 "prog.l" 807 | return GE; 808 | YY_BREAK 809 | case 6: 810 | YY_RULE_SETUP 811 | #line 17 "prog.l" 812 | return LE; 813 | YY_BREAK 814 | case 7: 815 | YY_RULE_SETUP 816 | #line 18 "prog.l" 817 | return EQ; 818 | YY_BREAK 819 | case 8: 820 | YY_RULE_SETUP 821 | #line 19 "prog.l" 822 | return ASSIGN; 823 | YY_BREAK 824 | case 9: 825 | YY_RULE_SETUP 826 | #line 20 "prog.l" 827 | return AND; 828 | YY_BREAK 829 | case 10: 830 | YY_RULE_SETUP 831 | #line 21 "prog.l" 832 | return OR; 833 | YY_BREAK 834 | case 11: 835 | YY_RULE_SETUP 836 | #line 22 "prog.l" 837 | return XOR; 838 | YY_BREAK 839 | case 12: 840 | YY_RULE_SETUP 841 | #line 23 "prog.l" 842 | return SWITCH; 843 | YY_BREAK 844 | case 13: 845 | YY_RULE_SETUP 846 | #line 24 "prog.l" 847 | return IF; 848 | YY_BREAK 849 | case 14: 850 | YY_RULE_SETUP 851 | #line 25 "prog.l" 852 | return ELSE; 853 | YY_BREAK 854 | case 15: 855 | YY_RULE_SETUP 856 | #line 26 "prog.l" 857 | return BREAK; 858 | YY_BREAK 859 | case 16: 860 | YY_RULE_SETUP 861 | #line 27 "prog.l" 862 | return CHAR; 863 | YY_BREAK 864 | case 17: 865 | YY_RULE_SETUP 866 | #line 28 "prog.l" 867 | return INT; 868 | YY_BREAK 869 | case 18: 870 | YY_RULE_SETUP 871 | #line 29 "prog.l" 872 | return WHILE; 873 | YY_BREAK 874 | case 19: 875 | YY_RULE_SETUP 876 | #line 30 "prog.l" 877 | return FLOAT; 878 | YY_BREAK 879 | case 20: 880 | YY_RULE_SETUP 881 | #line 31 "prog.l" 882 | return CASE; 883 | YY_BREAK 884 | case 21: 885 | YY_RULE_SETUP 886 | #line 32 "prog.l" 887 | return DOUBLE; 888 | YY_BREAK 889 | case 22: 890 | YY_RULE_SETUP 891 | #line 33 "prog.l" 892 | return IDENTIFIER; 893 | YY_BREAK 894 | case 23: 895 | YY_RULE_SETUP 896 | #line 34 "prog.l" 897 | return NUM; 898 | YY_BREAK 899 | case 24: 900 | YY_RULE_SETUP 901 | #line 35 "prog.l" 902 | return SEMICOLON; 903 | YY_BREAK 904 | case 25: 905 | YY_RULE_SETUP 906 | #line 36 "prog.l" 907 | return COMMA; 908 | YY_BREAK 909 | case 26: 910 | YY_RULE_SETUP 911 | #line 37 "prog.l" 912 | return OP; 913 | YY_BREAK 914 | case 27: 915 | YY_RULE_SETUP 916 | #line 38 "prog.l" 917 | return CP; 918 | YY_BREAK 919 | case 28: 920 | YY_RULE_SETUP 921 | #line 39 "prog.l" 922 | return OB; 923 | YY_BREAK 924 | case 29: 925 | YY_RULE_SETUP 926 | #line 40 "prog.l" 927 | return CB; 928 | YY_BREAK 929 | case 30: 930 | YY_RULE_SETUP 931 | #line 41 "prog.l" 932 | ; 933 | YY_BREAK 934 | case 31: 935 | /* rule 31 can match eol */ 936 | YY_RULE_SETUP 937 | #line 42 "prog.l" 938 | ; 939 | YY_BREAK 940 | case 32: 941 | YY_RULE_SETUP 942 | #line 43 "prog.l" 943 | ECHO; 944 | YY_BREAK 945 | #line 946 "lex.yy.c" 946 | case YY_STATE_EOF(INITIAL): 947 | yyterminate(); 948 | 949 | case YY_END_OF_BUFFER: 950 | { 951 | /* Amount of text matched not including the EOB char. */ 952 | int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; 953 | 954 | /* Undo the effects of YY_DO_BEFORE_ACTION. */ 955 | *yy_cp = (yy_hold_char); 956 | YY_RESTORE_YY_MORE_OFFSET 957 | 958 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) 959 | { 960 | /* We're scanning a new file or input source. It's 961 | * possible that this happened because the user 962 | * just pointed yyin at a new source and called 963 | * yylex(). If so, then we have to assure 964 | * consistency between YY_CURRENT_BUFFER and our 965 | * globals. Here is the right place to do so, because 966 | * this is the first action (other than possibly a 967 | * back-up) that will match for the new input source. 968 | */ 969 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 970 | YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; 971 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; 972 | } 973 | 974 | /* Note that here we test for yy_c_buf_p "<=" to the position 975 | * of the first EOB in the buffer, since yy_c_buf_p will 976 | * already have been incremented past the NUL character 977 | * (since all states make transitions on EOB to the 978 | * end-of-buffer state). Contrast this with the test 979 | * in input(). 980 | */ 981 | if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) 982 | { /* This was really a NUL. */ 983 | yy_state_type yy_next_state; 984 | 985 | (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; 986 | 987 | yy_current_state = yy_get_previous_state( ); 988 | 989 | /* Okay, we're now positioned to make the NUL 990 | * transition. We couldn't have 991 | * yy_get_previous_state() go ahead and do it 992 | * for us because it doesn't know how to deal 993 | * with the possibility of jamming (and we don't 994 | * want to build jamming into it because then it 995 | * will run more slowly). 996 | */ 997 | 998 | yy_next_state = yy_try_NUL_trans( yy_current_state ); 999 | 1000 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1001 | 1002 | if ( yy_next_state ) 1003 | { 1004 | /* Consume the NUL. */ 1005 | yy_cp = ++(yy_c_buf_p); 1006 | yy_current_state = yy_next_state; 1007 | goto yy_match; 1008 | } 1009 | 1010 | else 1011 | { 1012 | yy_cp = (yy_c_buf_p); 1013 | goto yy_find_action; 1014 | } 1015 | } 1016 | 1017 | else switch ( yy_get_next_buffer( ) ) 1018 | { 1019 | case EOB_ACT_END_OF_FILE: 1020 | { 1021 | (yy_did_buffer_switch_on_eof) = 0; 1022 | 1023 | if ( yywrap( ) ) 1024 | { 1025 | /* Note: because we've taken care in 1026 | * yy_get_next_buffer() to have set up 1027 | * yytext, we can now set up 1028 | * yy_c_buf_p so that if some total 1029 | * hoser (like flex itself) wants to 1030 | * call the scanner after we return the 1031 | * YY_NULL, it'll still work - another 1032 | * YY_NULL will get returned. 1033 | */ 1034 | (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; 1035 | 1036 | yy_act = YY_STATE_EOF(YY_START); 1037 | goto do_action; 1038 | } 1039 | 1040 | else 1041 | { 1042 | if ( ! (yy_did_buffer_switch_on_eof) ) 1043 | YY_NEW_FILE; 1044 | } 1045 | break; 1046 | } 1047 | 1048 | case EOB_ACT_CONTINUE_SCAN: 1049 | (yy_c_buf_p) = 1050 | (yytext_ptr) + yy_amount_of_matched_text; 1051 | 1052 | yy_current_state = yy_get_previous_state( ); 1053 | 1054 | yy_cp = (yy_c_buf_p); 1055 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1056 | goto yy_match; 1057 | 1058 | case EOB_ACT_LAST_MATCH: 1059 | (yy_c_buf_p) = 1060 | &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; 1061 | 1062 | yy_current_state = yy_get_previous_state( ); 1063 | 1064 | yy_cp = (yy_c_buf_p); 1065 | yy_bp = (yytext_ptr) + YY_MORE_ADJ; 1066 | goto yy_find_action; 1067 | } 1068 | break; 1069 | } 1070 | 1071 | default: 1072 | YY_FATAL_ERROR( 1073 | "fatal flex scanner internal error--no action found" ); 1074 | } /* end of action switch */ 1075 | } /* end of scanning one token */ 1076 | } /* end of user's declarations */ 1077 | } /* end of yylex */ 1078 | 1079 | /* yy_get_next_buffer - try to read in a new buffer 1080 | * 1081 | * Returns a code representing an action: 1082 | * EOB_ACT_LAST_MATCH - 1083 | * EOB_ACT_CONTINUE_SCAN - continue scanning from current position 1084 | * EOB_ACT_END_OF_FILE - end of file 1085 | */ 1086 | static int yy_get_next_buffer (void) 1087 | { 1088 | register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; 1089 | register char *source = (yytext_ptr); 1090 | register int number_to_move, i; 1091 | int ret_val; 1092 | 1093 | if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) 1094 | YY_FATAL_ERROR( 1095 | "fatal flex scanner internal error--end of buffer missed" ); 1096 | 1097 | if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) 1098 | { /* Don't try to fill the buffer, so this is an EOF. */ 1099 | if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) 1100 | { 1101 | /* We matched a single character, the EOB, so 1102 | * treat this as a final EOF. 1103 | */ 1104 | return EOB_ACT_END_OF_FILE; 1105 | } 1106 | 1107 | else 1108 | { 1109 | /* We matched some text prior to the EOB, first 1110 | * process it. 1111 | */ 1112 | return EOB_ACT_LAST_MATCH; 1113 | } 1114 | } 1115 | 1116 | /* Try to read more data. */ 1117 | 1118 | /* First move last chars to start of buffer. */ 1119 | number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; 1120 | 1121 | for ( i = 0; i < number_to_move; ++i ) 1122 | *(dest++) = *(source++); 1123 | 1124 | if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) 1125 | /* don't do the read, it's not guaranteed to return an EOF, 1126 | * just force an EOF 1127 | */ 1128 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; 1129 | 1130 | else 1131 | { 1132 | yy_size_t num_to_read = 1133 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; 1134 | 1135 | while ( num_to_read <= 0 ) 1136 | { /* Not enough room in the buffer - grow it. */ 1137 | 1138 | /* just a shorter name for the current buffer */ 1139 | YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; 1140 | 1141 | int yy_c_buf_p_offset = 1142 | (int) ((yy_c_buf_p) - b->yy_ch_buf); 1143 | 1144 | if ( b->yy_is_our_buffer ) 1145 | { 1146 | yy_size_t new_size = b->yy_buf_size * 2; 1147 | 1148 | if ( new_size <= 0 ) 1149 | b->yy_buf_size += b->yy_buf_size / 8; 1150 | else 1151 | b->yy_buf_size *= 2; 1152 | 1153 | b->yy_ch_buf = (char *) 1154 | /* Include room in for 2 EOB chars. */ 1155 | yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); 1156 | } 1157 | else 1158 | /* Can't grow it, we don't own it. */ 1159 | b->yy_ch_buf = 0; 1160 | 1161 | if ( ! b->yy_ch_buf ) 1162 | YY_FATAL_ERROR( 1163 | "fatal error - scanner input buffer overflow" ); 1164 | 1165 | (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; 1166 | 1167 | num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - 1168 | number_to_move - 1; 1169 | 1170 | } 1171 | 1172 | if ( num_to_read > YY_READ_BUF_SIZE ) 1173 | num_to_read = YY_READ_BUF_SIZE; 1174 | 1175 | /* Read in more data. */ 1176 | YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), 1177 | (yy_n_chars), num_to_read ); 1178 | 1179 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1180 | } 1181 | 1182 | if ( (yy_n_chars) == 0 ) 1183 | { 1184 | if ( number_to_move == YY_MORE_ADJ ) 1185 | { 1186 | ret_val = EOB_ACT_END_OF_FILE; 1187 | yyrestart(yyin ); 1188 | } 1189 | 1190 | else 1191 | { 1192 | ret_val = EOB_ACT_LAST_MATCH; 1193 | YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = 1194 | YY_BUFFER_EOF_PENDING; 1195 | } 1196 | } 1197 | 1198 | else 1199 | ret_val = EOB_ACT_CONTINUE_SCAN; 1200 | 1201 | if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { 1202 | /* Extend the array by 50%, plus the number we really need. */ 1203 | yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); 1204 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); 1205 | if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) 1206 | YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); 1207 | } 1208 | 1209 | (yy_n_chars) += number_to_move; 1210 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; 1211 | YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; 1212 | 1213 | (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; 1214 | 1215 | return ret_val; 1216 | } 1217 | 1218 | /* yy_get_previous_state - get the state just before the EOB char was reached */ 1219 | 1220 | static yy_state_type yy_get_previous_state (void) 1221 | { 1222 | register yy_state_type yy_current_state; 1223 | register char *yy_cp; 1224 | 1225 | yy_current_state = (yy_start); 1226 | 1227 | for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) 1228 | { 1229 | register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); 1230 | if ( yy_accept[yy_current_state] ) 1231 | { 1232 | (yy_last_accepting_state) = yy_current_state; 1233 | (yy_last_accepting_cpos) = yy_cp; 1234 | } 1235 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1236 | { 1237 | yy_current_state = (int) yy_def[yy_current_state]; 1238 | if ( yy_current_state >= 75 ) 1239 | yy_c = yy_meta[(unsigned int) yy_c]; 1240 | } 1241 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1242 | } 1243 | 1244 | return yy_current_state; 1245 | } 1246 | 1247 | /* yy_try_NUL_trans - try to make a transition on the NUL character 1248 | * 1249 | * synopsis 1250 | * next_state = yy_try_NUL_trans( current_state ); 1251 | */ 1252 | static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) 1253 | { 1254 | register int yy_is_jam; 1255 | register char *yy_cp = (yy_c_buf_p); 1256 | 1257 | register YY_CHAR yy_c = 1; 1258 | if ( yy_accept[yy_current_state] ) 1259 | { 1260 | (yy_last_accepting_state) = yy_current_state; 1261 | (yy_last_accepting_cpos) = yy_cp; 1262 | } 1263 | while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) 1264 | { 1265 | yy_current_state = (int) yy_def[yy_current_state]; 1266 | if ( yy_current_state >= 75 ) 1267 | yy_c = yy_meta[(unsigned int) yy_c]; 1268 | } 1269 | yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; 1270 | yy_is_jam = (yy_current_state == 74); 1271 | 1272 | return yy_is_jam ? 0 : yy_current_state; 1273 | } 1274 | 1275 | static void yyunput (int c, register char * yy_bp ) 1276 | { 1277 | register char *yy_cp; 1278 | 1279 | yy_cp = (yy_c_buf_p); 1280 | 1281 | /* undo effects of setting up yytext */ 1282 | *yy_cp = (yy_hold_char); 1283 | 1284 | if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) 1285 | { /* need to shift things up to make room */ 1286 | /* +2 for EOB chars. */ 1287 | register yy_size_t number_to_move = (yy_n_chars) + 2; 1288 | register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ 1289 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; 1290 | register char *source = 1291 | &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; 1292 | 1293 | while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) 1294 | *--dest = *--source; 1295 | 1296 | yy_cp += (int) (dest - source); 1297 | yy_bp += (int) (dest - source); 1298 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = 1299 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; 1300 | 1301 | if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) 1302 | YY_FATAL_ERROR( "flex scanner push-back overflow" ); 1303 | } 1304 | 1305 | *--yy_cp = (char) c; 1306 | 1307 | (yytext_ptr) = yy_bp; 1308 | (yy_hold_char) = *yy_cp; 1309 | (yy_c_buf_p) = yy_cp; 1310 | } 1311 | 1312 | #ifndef YY_NO_INPUT 1313 | #ifdef __cplusplus 1314 | static int yyinput (void) 1315 | #else 1316 | static int input (void) 1317 | #endif 1318 | 1319 | { 1320 | int c; 1321 | 1322 | *(yy_c_buf_p) = (yy_hold_char); 1323 | 1324 | if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) 1325 | { 1326 | /* yy_c_buf_p now points to the character we want to return. 1327 | * If this occurs *before* the EOB characters, then it's a 1328 | * valid NUL; if not, then we've hit the end of the buffer. 1329 | */ 1330 | if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) 1331 | /* This was really a NUL. */ 1332 | *(yy_c_buf_p) = '\0'; 1333 | 1334 | else 1335 | { /* need more input */ 1336 | yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); 1337 | ++(yy_c_buf_p); 1338 | 1339 | switch ( yy_get_next_buffer( ) ) 1340 | { 1341 | case EOB_ACT_LAST_MATCH: 1342 | /* This happens because yy_g_n_b() 1343 | * sees that we've accumulated a 1344 | * token and flags that we need to 1345 | * try matching the token before 1346 | * proceeding. But for input(), 1347 | * there's no matching to consider. 1348 | * So convert the EOB_ACT_LAST_MATCH 1349 | * to EOB_ACT_END_OF_FILE. 1350 | */ 1351 | 1352 | /* Reset buffer status. */ 1353 | yyrestart(yyin ); 1354 | 1355 | /*FALLTHROUGH*/ 1356 | 1357 | case EOB_ACT_END_OF_FILE: 1358 | { 1359 | if ( yywrap( ) ) 1360 | return EOF; 1361 | 1362 | if ( ! (yy_did_buffer_switch_on_eof) ) 1363 | YY_NEW_FILE; 1364 | #ifdef __cplusplus 1365 | return yyinput(); 1366 | #else 1367 | return input(); 1368 | #endif 1369 | } 1370 | 1371 | case EOB_ACT_CONTINUE_SCAN: 1372 | (yy_c_buf_p) = (yytext_ptr) + offset; 1373 | break; 1374 | } 1375 | } 1376 | } 1377 | 1378 | c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ 1379 | *(yy_c_buf_p) = '\0'; /* preserve yytext */ 1380 | (yy_hold_char) = *++(yy_c_buf_p); 1381 | 1382 | return c; 1383 | } 1384 | #endif /* ifndef YY_NO_INPUT */ 1385 | 1386 | /** Immediately switch to a different input stream. 1387 | * @param input_file A readable stream. 1388 | * 1389 | * @note This function does not reset the start condition to @c INITIAL . 1390 | */ 1391 | void yyrestart (FILE * input_file ) 1392 | { 1393 | 1394 | if ( ! YY_CURRENT_BUFFER ){ 1395 | yyensure_buffer_stack (); 1396 | YY_CURRENT_BUFFER_LVALUE = 1397 | yy_create_buffer(yyin,YY_BUF_SIZE ); 1398 | } 1399 | 1400 | yy_init_buffer(YY_CURRENT_BUFFER,input_file ); 1401 | yy_load_buffer_state( ); 1402 | } 1403 | 1404 | /** Switch to a different input buffer. 1405 | * @param new_buffer The new input buffer. 1406 | * 1407 | */ 1408 | void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) 1409 | { 1410 | 1411 | /* TODO. We should be able to replace this entire function body 1412 | * with 1413 | * yypop_buffer_state(); 1414 | * yypush_buffer_state(new_buffer); 1415 | */ 1416 | yyensure_buffer_stack (); 1417 | if ( YY_CURRENT_BUFFER == new_buffer ) 1418 | return; 1419 | 1420 | if ( YY_CURRENT_BUFFER ) 1421 | { 1422 | /* Flush out information for old buffer. */ 1423 | *(yy_c_buf_p) = (yy_hold_char); 1424 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); 1425 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1426 | } 1427 | 1428 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1429 | yy_load_buffer_state( ); 1430 | 1431 | /* We don't actually know whether we did this switch during 1432 | * EOF (yywrap()) processing, but the only time this flag 1433 | * is looked at is after yywrap() is called, so it's safe 1434 | * to go ahead and always set it. 1435 | */ 1436 | (yy_did_buffer_switch_on_eof) = 1; 1437 | } 1438 | 1439 | static void yy_load_buffer_state (void) 1440 | { 1441 | (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; 1442 | (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; 1443 | yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; 1444 | (yy_hold_char) = *(yy_c_buf_p); 1445 | } 1446 | 1447 | /** Allocate and initialize an input buffer state. 1448 | * @param file A readable stream. 1449 | * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. 1450 | * 1451 | * @return the allocated buffer state. 1452 | */ 1453 | YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) 1454 | { 1455 | YY_BUFFER_STATE b; 1456 | 1457 | b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); 1458 | if ( ! b ) 1459 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); 1460 | 1461 | b->yy_buf_size = size; 1462 | 1463 | /* yy_ch_buf has to be 2 characters longer than the size given because 1464 | * we need to put in 2 end-of-buffer characters. 1465 | */ 1466 | b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); 1467 | if ( ! b->yy_ch_buf ) 1468 | YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); 1469 | 1470 | b->yy_is_our_buffer = 1; 1471 | 1472 | yy_init_buffer(b,file ); 1473 | 1474 | return b; 1475 | } 1476 | 1477 | /** Destroy the buffer. 1478 | * @param b a buffer created with yy_create_buffer() 1479 | * 1480 | */ 1481 | void yy_delete_buffer (YY_BUFFER_STATE b ) 1482 | { 1483 | 1484 | if ( ! b ) 1485 | return; 1486 | 1487 | if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ 1488 | YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; 1489 | 1490 | if ( b->yy_is_our_buffer ) 1491 | yyfree((void *) b->yy_ch_buf ); 1492 | 1493 | yyfree((void *) b ); 1494 | } 1495 | 1496 | /* Initializes or reinitializes a buffer. 1497 | * This function is sometimes called more than once on the same buffer, 1498 | * such as during a yyrestart() or at EOF. 1499 | */ 1500 | static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) 1501 | 1502 | { 1503 | int oerrno = errno; 1504 | 1505 | yy_flush_buffer(b ); 1506 | 1507 | b->yy_input_file = file; 1508 | b->yy_fill_buffer = 1; 1509 | 1510 | /* If b is the current buffer, then yy_init_buffer was _probably_ 1511 | * called from yyrestart() or through yy_get_next_buffer. 1512 | * In that case, we don't want to reset the lineno or column. 1513 | */ 1514 | if (b != YY_CURRENT_BUFFER){ 1515 | b->yy_bs_lineno = 1; 1516 | b->yy_bs_column = 0; 1517 | } 1518 | 1519 | b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; 1520 | 1521 | errno = oerrno; 1522 | } 1523 | 1524 | /** Discard all buffered characters. On the next scan, YY_INPUT will be called. 1525 | * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. 1526 | * 1527 | */ 1528 | void yy_flush_buffer (YY_BUFFER_STATE b ) 1529 | { 1530 | if ( ! b ) 1531 | return; 1532 | 1533 | b->yy_n_chars = 0; 1534 | 1535 | /* We always need two end-of-buffer characters. The first causes 1536 | * a transition to the end-of-buffer state. The second causes 1537 | * a jam in that state. 1538 | */ 1539 | b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; 1540 | b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; 1541 | 1542 | b->yy_buf_pos = &b->yy_ch_buf[0]; 1543 | 1544 | b->yy_at_bol = 1; 1545 | b->yy_buffer_status = YY_BUFFER_NEW; 1546 | 1547 | if ( b == YY_CURRENT_BUFFER ) 1548 | yy_load_buffer_state( ); 1549 | } 1550 | 1551 | /** Pushes the new state onto the stack. The new state becomes 1552 | * the current state. This function will allocate the stack 1553 | * if necessary. 1554 | * @param new_buffer The new state. 1555 | * 1556 | */ 1557 | void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) 1558 | { 1559 | if (new_buffer == NULL) 1560 | return; 1561 | 1562 | yyensure_buffer_stack(); 1563 | 1564 | /* This block is copied from yy_switch_to_buffer. */ 1565 | if ( YY_CURRENT_BUFFER ) 1566 | { 1567 | /* Flush out information for old buffer. */ 1568 | *(yy_c_buf_p) = (yy_hold_char); 1569 | YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); 1570 | YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); 1571 | } 1572 | 1573 | /* Only push if top exists. Otherwise, replace top. */ 1574 | if (YY_CURRENT_BUFFER) 1575 | (yy_buffer_stack_top)++; 1576 | YY_CURRENT_BUFFER_LVALUE = new_buffer; 1577 | 1578 | /* copied from yy_switch_to_buffer. */ 1579 | yy_load_buffer_state( ); 1580 | (yy_did_buffer_switch_on_eof) = 1; 1581 | } 1582 | 1583 | /** Removes and deletes the top of the stack, if present. 1584 | * The next element becomes the new top. 1585 | * 1586 | */ 1587 | void yypop_buffer_state (void) 1588 | { 1589 | if (!YY_CURRENT_BUFFER) 1590 | return; 1591 | 1592 | yy_delete_buffer(YY_CURRENT_BUFFER ); 1593 | YY_CURRENT_BUFFER_LVALUE = NULL; 1594 | if ((yy_buffer_stack_top) > 0) 1595 | --(yy_buffer_stack_top); 1596 | 1597 | if (YY_CURRENT_BUFFER) { 1598 | yy_load_buffer_state( ); 1599 | (yy_did_buffer_switch_on_eof) = 1; 1600 | } 1601 | } 1602 | 1603 | /* Allocates the stack if it does not exist. 1604 | * Guarantees space for at least one push. 1605 | */ 1606 | static void yyensure_buffer_stack (void) 1607 | { 1608 | yy_size_t num_to_alloc; 1609 | 1610 | if (!(yy_buffer_stack)) { 1611 | 1612 | /* First allocation is just for 2 elements, since we don't know if this 1613 | * scanner will even need a stack. We use 2 instead of 1 to avoid an 1614 | * immediate realloc on the next call. 1615 | */ 1616 | num_to_alloc = 1; 1617 | (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc 1618 | (num_to_alloc * sizeof(struct yy_buffer_state*) 1619 | ); 1620 | if ( ! (yy_buffer_stack) ) 1621 | YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); 1622 | 1623 | memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); 1624 | 1625 | (yy_buffer_stack_max) = num_to_alloc; 1626 | (yy_buffer_stack_top) = 0; 1627 | return; 1628 | } 1629 | 1630 | if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ 1631 | 1632 | /* Increase the buffer to prepare for a possible push. */ 1633 | int grow_size = 8 /* arbitrary grow size */; 1634 | 1635 | num_to_alloc = (yy_buffer_stack_max) + grow_size; 1636 | (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc 1637 | ((yy_buffer_stack), 1638 | num_to_alloc * sizeof(struct yy_buffer_state*) 1639 | ); 1640 | if ( ! (yy_buffer_stack) ) 1641 | YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); 1642 | 1643 | /* zero only the new slots.*/ 1644 | memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); 1645 | (yy_buffer_stack_max) = num_to_alloc; 1646 | } 1647 | } 1648 | 1649 | /** Setup the input buffer state to scan directly from a user-specified character buffer. 1650 | * @param base the character buffer 1651 | * @param size the size in bytes of the character buffer 1652 | * 1653 | * @return the newly allocated buffer state object. 1654 | */ 1655 | YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) 1656 | { 1657 | YY_BUFFER_STATE b; 1658 | 1659 | if ( size < 2 || 1660 | base[size-2] != YY_END_OF_BUFFER_CHAR || 1661 | base[size-1] != YY_END_OF_BUFFER_CHAR ) 1662 | /* They forgot to leave room for the EOB's. */ 1663 | return 0; 1664 | 1665 | b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); 1666 | if ( ! b ) 1667 | YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); 1668 | 1669 | b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ 1670 | b->yy_buf_pos = b->yy_ch_buf = base; 1671 | b->yy_is_our_buffer = 0; 1672 | b->yy_input_file = 0; 1673 | b->yy_n_chars = b->yy_buf_size; 1674 | b->yy_is_interactive = 0; 1675 | b->yy_at_bol = 1; 1676 | b->yy_fill_buffer = 0; 1677 | b->yy_buffer_status = YY_BUFFER_NEW; 1678 | 1679 | yy_switch_to_buffer(b ); 1680 | 1681 | return b; 1682 | } 1683 | 1684 | /** Setup the input buffer state to scan a string. The next call to yylex() will 1685 | * scan from a @e copy of @a str. 1686 | * @param yystr a NUL-terminated string to scan 1687 | * 1688 | * @return the newly allocated buffer state object. 1689 | * @note If you want to scan bytes that may contain NUL values, then use 1690 | * yy_scan_bytes() instead. 1691 | */ 1692 | YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) 1693 | { 1694 | 1695 | return yy_scan_bytes(yystr,strlen(yystr) ); 1696 | } 1697 | 1698 | /** Setup the input buffer state to scan the given bytes. The next call to yylex() will 1699 | * scan from a @e copy of @a bytes. 1700 | * @param yybytes the byte buffer to scan 1701 | * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. 1702 | * 1703 | * @return the newly allocated buffer state object. 1704 | */ 1705 | YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len ) 1706 | { 1707 | YY_BUFFER_STATE b; 1708 | char *buf; 1709 | yy_size_t n; 1710 | yy_size_t i; 1711 | 1712 | /* Get memory for full buffer, including space for trailing EOB's. */ 1713 | n = _yybytes_len + 2; 1714 | buf = (char *) yyalloc(n ); 1715 | if ( ! buf ) 1716 | YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); 1717 | 1718 | for ( i = 0; i < _yybytes_len; ++i ) 1719 | buf[i] = yybytes[i]; 1720 | 1721 | buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; 1722 | 1723 | b = yy_scan_buffer(buf,n ); 1724 | if ( ! b ) 1725 | YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); 1726 | 1727 | /* It's okay to grow etc. this buffer, and we should throw it 1728 | * away when we're done. 1729 | */ 1730 | b->yy_is_our_buffer = 1; 1731 | 1732 | return b; 1733 | } 1734 | 1735 | #ifndef YY_EXIT_FAILURE 1736 | #define YY_EXIT_FAILURE 2 1737 | #endif 1738 | 1739 | static void yy_fatal_error (yyconst char* msg ) 1740 | { 1741 | (void) fprintf( stderr, "%s\n", msg ); 1742 | exit( YY_EXIT_FAILURE ); 1743 | } 1744 | 1745 | /* Redefine yyless() so it works in section 3 code. */ 1746 | 1747 | #undef yyless 1748 | #define yyless(n) \ 1749 | do \ 1750 | { \ 1751 | /* Undo effects of setting up yytext. */ \ 1752 | int yyless_macro_arg = (n); \ 1753 | YY_LESS_LINENO(yyless_macro_arg);\ 1754 | yytext[yyleng] = (yy_hold_char); \ 1755 | (yy_c_buf_p) = yytext + yyless_macro_arg; \ 1756 | (yy_hold_char) = *(yy_c_buf_p); \ 1757 | *(yy_c_buf_p) = '\0'; \ 1758 | yyleng = yyless_macro_arg; \ 1759 | } \ 1760 | while ( 0 ) 1761 | 1762 | /* Accessor methods (get/set functions) to struct members. */ 1763 | 1764 | /** Get the current line number. 1765 | * 1766 | */ 1767 | int yyget_lineno (void) 1768 | { 1769 | 1770 | return yylineno; 1771 | } 1772 | 1773 | /** Get the input stream. 1774 | * 1775 | */ 1776 | FILE *yyget_in (void) 1777 | { 1778 | return yyin; 1779 | } 1780 | 1781 | /** Get the output stream. 1782 | * 1783 | */ 1784 | FILE *yyget_out (void) 1785 | { 1786 | return yyout; 1787 | } 1788 | 1789 | /** Get the length of the current token. 1790 | * 1791 | */ 1792 | yy_size_t yyget_leng (void) 1793 | { 1794 | return yyleng; 1795 | } 1796 | 1797 | /** Get the current token. 1798 | * 1799 | */ 1800 | 1801 | char *yyget_text (void) 1802 | { 1803 | return yytext; 1804 | } 1805 | 1806 | /** Set the current line number. 1807 | * @param line_number 1808 | * 1809 | */ 1810 | void yyset_lineno (int line_number ) 1811 | { 1812 | 1813 | yylineno = line_number; 1814 | } 1815 | 1816 | /** Set the input stream. This does not discard the current 1817 | * input buffer. 1818 | * @param in_str A readable stream. 1819 | * 1820 | * @see yy_switch_to_buffer 1821 | */ 1822 | void yyset_in (FILE * in_str ) 1823 | { 1824 | yyin = in_str ; 1825 | } 1826 | 1827 | void yyset_out (FILE * out_str ) 1828 | { 1829 | yyout = out_str ; 1830 | } 1831 | 1832 | int yyget_debug (void) 1833 | { 1834 | return yy_flex_debug; 1835 | } 1836 | 1837 | void yyset_debug (int bdebug ) 1838 | { 1839 | yy_flex_debug = bdebug ; 1840 | } 1841 | 1842 | static int yy_init_globals (void) 1843 | { 1844 | /* Initialization is the same as for the non-reentrant scanner. 1845 | * This function is called from yylex_destroy(), so don't allocate here. 1846 | */ 1847 | 1848 | (yy_buffer_stack) = 0; 1849 | (yy_buffer_stack_top) = 0; 1850 | (yy_buffer_stack_max) = 0; 1851 | (yy_c_buf_p) = (char *) 0; 1852 | (yy_init) = 0; 1853 | (yy_start) = 0; 1854 | 1855 | /* Defined in main.c */ 1856 | #ifdef YY_STDINIT 1857 | yyin = stdin; 1858 | yyout = stdout; 1859 | #else 1860 | yyin = (FILE *) 0; 1861 | yyout = (FILE *) 0; 1862 | #endif 1863 | 1864 | /* For future reference: Set errno on error, since we are called by 1865 | * yylex_init() 1866 | */ 1867 | return 0; 1868 | } 1869 | 1870 | /* yylex_destroy is for both reentrant and non-reentrant scanners. */ 1871 | int yylex_destroy (void) 1872 | { 1873 | 1874 | /* Pop the buffer stack, destroying each element. */ 1875 | while(YY_CURRENT_BUFFER){ 1876 | yy_delete_buffer(YY_CURRENT_BUFFER ); 1877 | YY_CURRENT_BUFFER_LVALUE = NULL; 1878 | yypop_buffer_state(); 1879 | } 1880 | 1881 | /* Destroy the stack itself. */ 1882 | yyfree((yy_buffer_stack) ); 1883 | (yy_buffer_stack) = NULL; 1884 | 1885 | /* Reset the globals. This is important in a non-reentrant scanner so the next time 1886 | * yylex() is called, initialization will occur. */ 1887 | yy_init_globals( ); 1888 | 1889 | return 0; 1890 | } 1891 | 1892 | /* 1893 | * Internal utility routines. 1894 | */ 1895 | 1896 | #ifndef yytext_ptr 1897 | static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) 1898 | { 1899 | register int i; 1900 | for ( i = 0; i < n; ++i ) 1901 | s1[i] = s2[i]; 1902 | } 1903 | #endif 1904 | 1905 | #ifdef YY_NEED_STRLEN 1906 | static int yy_flex_strlen (yyconst char * s ) 1907 | { 1908 | register int n; 1909 | for ( n = 0; s[n]; ++n ) 1910 | ; 1911 | 1912 | return n; 1913 | } 1914 | #endif 1915 | 1916 | void *yyalloc (yy_size_t size ) 1917 | { 1918 | return (void *) malloc( size ); 1919 | } 1920 | 1921 | void *yyrealloc (void * ptr, yy_size_t size ) 1922 | { 1923 | /* The cast to (char *) in the following accommodates both 1924 | * implementations that use char* generic pointers, and those 1925 | * that use void* generic pointers. It works with the latter 1926 | * because both ANSI C and C++ allow castless assignment from 1927 | * any pointer type to void*, and deal with argument conversions 1928 | * as though doing an assignment. 1929 | */ 1930 | return (void *) realloc( (char *) ptr, size ); 1931 | } 1932 | 1933 | void yyfree (void * ptr ) 1934 | { 1935 | free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ 1936 | } 1937 | 1938 | #define YYTABLES_NAME "yytables" 1939 | 1940 | #line 42 "prog.l" 1941 | 1942 | 1943 | 1944 | 1945 | -------------------------------------------------------------------------------- /Syntax Analysis/prog.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | #include 6 | %} 7 | 8 | alpha [a-zA-Z] 9 | digit [0-9] 10 | 11 | %% 12 | "-" return SUB; 13 | "+" return ADD; 14 | "*" return MUL; 15 | "/" return DIV; 16 | ">=" return GE; 17 | "<=" return LE; 18 | "==" return EQ; 19 | "=" return ASSIGN; 20 | "&&" return AND; 21 | "||" return OR; 22 | "^" return XOR; 23 | "switch" return SWITCH; 24 | "if" return IF; 25 | "else" return ELSE; 26 | "break" return BREAK; 27 | "char" return CHAR; 28 | "int" return INT; 29 | "while" return WHILE; 30 | "float" return FLOAT; 31 | "case" return CASE; 32 | "double" return DOUBLE; 33 | {alpha}({alpha}|{digit})* return IDENTIFIER; 34 | {digit}+ return NUM; 35 | ";" return SEMICOLON; 36 | "," return COMMA; 37 | "(" return OP; 38 | ")" return CP; 39 | "{" return OB; 40 | "}" return CB; 41 | . ; 42 | [ \n] ; 43 | %% 44 | 45 | -------------------------------------------------------------------------------- /Syntax Analysis/prog.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | %} 6 | 7 | %token IDENTIFIER NUM 8 | %token CHAR INT FLOAT DOUBLE 9 | %token EQ LE GE AND OR XOR ASSIGN 10 | %token IF ELSE SWITCH BREAK WHILE CASE 11 | %token ADD SUB MUL DIV 12 | %token SEMICOLON COMMA 13 | %token OP CP OB CB 14 | 15 | %start program 16 | 17 | %% 18 | program 19 | : declarations function {printf("Input accepted\n");exit(0);} 20 | ; 21 | declarations 22 | : type variables SEMICOLON 23 | | 24 | ; 25 | type 26 | : INT 27 | | CHAR 28 | | FLOAT 29 | | DOUBLE 30 | ; 31 | variables 32 | : IDENTIFIER 33 | | IDENTIFIER COMMA variables 34 | ; 35 | function 36 | : type IDENTIFIER OP argumentlist CP OB statements CB 37 | ; 38 | argumentlist 39 | : variables 40 | ; 41 | statements 42 | : expressions SEMICOLON 43 | ; 44 | expressions 45 | : IDENTIFIER ASSIGN expressions 46 | | IDENTIFIER 47 | | NUM 48 | ; 49 | %% 50 | 51 | #include "lex.yy.c" 52 | int main() 53 | { 54 | yyin=fopen("input.c","r"); 55 | yyparse(); 56 | fclose(yyin); 57 | return 0; 58 | } 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Syntax Analysis/y.tab.c: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.0.2. */ 2 | 3 | /* Bison implementation for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | /* C LALR(1) parser skeleton written by Richard Stallman, by 34 | simplifying the original so-called "semantic" parser. */ 35 | 36 | /* All symbols defined below should begin with yy or YY, to avoid 37 | infringing on user name space. This should be done even for local 38 | variables, as they might otherwise be expanded by user macros. 39 | There are some unavoidable exceptions within include files to 40 | define necessary library symbols; they are noted "INFRINGES ON 41 | USER NAME SPACE" below. */ 42 | 43 | /* Identify Bison output. */ 44 | #define YYBISON 1 45 | 46 | /* Bison version. */ 47 | #define YYBISON_VERSION "3.0.2" 48 | 49 | /* Skeleton name. */ 50 | #define YYSKELETON_NAME "yacc.c" 51 | 52 | /* Pure parsers. */ 53 | #define YYPURE 0 54 | 55 | /* Push parsers. */ 56 | #define YYPUSH 0 57 | 58 | /* Pull parsers. */ 59 | #define YYPULL 1 60 | 61 | 62 | 63 | 64 | /* Copy the first part of user declarations. */ 65 | #line 1 "prog.y" /* yacc.c:339 */ 66 | 67 | #include 68 | #include 69 | #include 70 | 71 | #line 72 "y.tab.c" /* yacc.c:339 */ 72 | 73 | # ifndef YY_NULLPTR 74 | # if defined __cplusplus && 201103L <= __cplusplus 75 | # define YY_NULLPTR nullptr 76 | # else 77 | # define YY_NULLPTR 0 78 | # endif 79 | # endif 80 | 81 | /* Enabling verbose error messages. */ 82 | #ifdef YYERROR_VERBOSE 83 | # undef YYERROR_VERBOSE 84 | # define YYERROR_VERBOSE 1 85 | #else 86 | # define YYERROR_VERBOSE 0 87 | #endif 88 | 89 | 90 | /* Debug traces. */ 91 | #ifndef YYDEBUG 92 | # define YYDEBUG 0 93 | #endif 94 | #if YYDEBUG 95 | extern int yydebug; 96 | #endif 97 | 98 | /* Token type. */ 99 | #ifndef YYTOKENTYPE 100 | # define YYTOKENTYPE 101 | enum yytokentype 102 | { 103 | IDENTIFIER = 258, 104 | NUM = 259, 105 | CHAR = 260, 106 | INT = 261, 107 | FLOAT = 262, 108 | DOUBLE = 263, 109 | EQ = 264, 110 | LE = 265, 111 | GE = 266, 112 | AND = 267, 113 | OR = 268, 114 | XOR = 269, 115 | ASSIGN = 270, 116 | IF = 271, 117 | ELSE = 272, 118 | SWITCH = 273, 119 | BREAK = 274, 120 | WHILE = 275, 121 | CASE = 276, 122 | ADD = 277, 123 | SUB = 278, 124 | MUL = 279, 125 | DIV = 280, 126 | SEMICOLON = 281, 127 | COMMA = 282, 128 | OP = 283, 129 | CP = 284, 130 | OB = 285, 131 | CB = 286 132 | }; 133 | #endif 134 | /* Tokens. */ 135 | #define IDENTIFIER 258 136 | #define NUM 259 137 | #define CHAR 260 138 | #define INT 261 139 | #define FLOAT 262 140 | #define DOUBLE 263 141 | #define EQ 264 142 | #define LE 265 143 | #define GE 266 144 | #define AND 267 145 | #define OR 268 146 | #define XOR 269 147 | #define ASSIGN 270 148 | #define IF 271 149 | #define ELSE 272 150 | #define SWITCH 273 151 | #define BREAK 274 152 | #define WHILE 275 153 | #define CASE 276 154 | #define ADD 277 155 | #define SUB 278 156 | #define MUL 279 157 | #define DIV 280 158 | #define SEMICOLON 281 159 | #define COMMA 282 160 | #define OP 283 161 | #define CP 284 162 | #define OB 285 163 | #define CB 286 164 | 165 | /* Value type. */ 166 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 167 | typedef int YYSTYPE; 168 | # define YYSTYPE_IS_TRIVIAL 1 169 | # define YYSTYPE_IS_DECLARED 1 170 | #endif 171 | 172 | 173 | extern YYSTYPE yylval; 174 | 175 | int yyparse (void); 176 | 177 | 178 | 179 | /* Copy the second part of user declarations. */ 180 | 181 | #line 182 "y.tab.c" /* yacc.c:358 */ 182 | 183 | #ifdef short 184 | # undef short 185 | #endif 186 | 187 | #ifdef YYTYPE_UINT8 188 | typedef YYTYPE_UINT8 yytype_uint8; 189 | #else 190 | typedef unsigned char yytype_uint8; 191 | #endif 192 | 193 | #ifdef YYTYPE_INT8 194 | typedef YYTYPE_INT8 yytype_int8; 195 | #else 196 | typedef signed char yytype_int8; 197 | #endif 198 | 199 | #ifdef YYTYPE_UINT16 200 | typedef YYTYPE_UINT16 yytype_uint16; 201 | #else 202 | typedef unsigned short int yytype_uint16; 203 | #endif 204 | 205 | #ifdef YYTYPE_INT16 206 | typedef YYTYPE_INT16 yytype_int16; 207 | #else 208 | typedef short int yytype_int16; 209 | #endif 210 | 211 | #ifndef YYSIZE_T 212 | # ifdef __SIZE_TYPE__ 213 | # define YYSIZE_T __SIZE_TYPE__ 214 | # elif defined size_t 215 | # define YYSIZE_T size_t 216 | # elif ! defined YYSIZE_T 217 | # include /* INFRINGES ON USER NAME SPACE */ 218 | # define YYSIZE_T size_t 219 | # else 220 | # define YYSIZE_T unsigned int 221 | # endif 222 | #endif 223 | 224 | #define YYSIZE_MAXIMUM ((YYSIZE_T) -1) 225 | 226 | #ifndef YY_ 227 | # if defined YYENABLE_NLS && YYENABLE_NLS 228 | # if ENABLE_NLS 229 | # include /* INFRINGES ON USER NAME SPACE */ 230 | # define YY_(Msgid) dgettext ("bison-runtime", Msgid) 231 | # endif 232 | # endif 233 | # ifndef YY_ 234 | # define YY_(Msgid) Msgid 235 | # endif 236 | #endif 237 | 238 | #ifndef YY_ATTRIBUTE 239 | # if (defined __GNUC__ \ 240 | && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ 241 | || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C 242 | # define YY_ATTRIBUTE(Spec) __attribute__(Spec) 243 | # else 244 | # define YY_ATTRIBUTE(Spec) /* empty */ 245 | # endif 246 | #endif 247 | 248 | #ifndef YY_ATTRIBUTE_PURE 249 | # define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) 250 | #endif 251 | 252 | #ifndef YY_ATTRIBUTE_UNUSED 253 | # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) 254 | #endif 255 | 256 | #if !defined _Noreturn \ 257 | && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) 258 | # if defined _MSC_VER && 1200 <= _MSC_VER 259 | # define _Noreturn __declspec (noreturn) 260 | # else 261 | # define _Noreturn YY_ATTRIBUTE ((__noreturn__)) 262 | # endif 263 | #endif 264 | 265 | /* Suppress unused-variable warnings by "using" E. */ 266 | #if ! defined lint || defined __GNUC__ 267 | # define YYUSE(E) ((void) (E)) 268 | #else 269 | # define YYUSE(E) /* empty */ 270 | #endif 271 | 272 | #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ 273 | /* Suppress an incorrect diagnostic about yylval being uninitialized. */ 274 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ 275 | _Pragma ("GCC diagnostic push") \ 276 | _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ 277 | _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") 278 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END \ 279 | _Pragma ("GCC diagnostic pop") 280 | #else 281 | # define YY_INITIAL_VALUE(Value) Value 282 | #endif 283 | #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 284 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 285 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END 286 | #endif 287 | #ifndef YY_INITIAL_VALUE 288 | # define YY_INITIAL_VALUE(Value) /* Nothing. */ 289 | #endif 290 | 291 | 292 | #if ! defined yyoverflow || YYERROR_VERBOSE 293 | 294 | /* The parser invokes alloca or malloc; define the necessary symbols. */ 295 | 296 | # ifdef YYSTACK_USE_ALLOCA 297 | # if YYSTACK_USE_ALLOCA 298 | # ifdef __GNUC__ 299 | # define YYSTACK_ALLOC __builtin_alloca 300 | # elif defined __BUILTIN_VA_ARG_INCR 301 | # include /* INFRINGES ON USER NAME SPACE */ 302 | # elif defined _AIX 303 | # define YYSTACK_ALLOC __alloca 304 | # elif defined _MSC_VER 305 | # include /* INFRINGES ON USER NAME SPACE */ 306 | # define alloca _alloca 307 | # else 308 | # define YYSTACK_ALLOC alloca 309 | # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS 310 | # include /* INFRINGES ON USER NAME SPACE */ 311 | /* Use EXIT_SUCCESS as a witness for stdlib.h. */ 312 | # ifndef EXIT_SUCCESS 313 | # define EXIT_SUCCESS 0 314 | # endif 315 | # endif 316 | # endif 317 | # endif 318 | # endif 319 | 320 | # ifdef YYSTACK_ALLOC 321 | /* Pacify GCC's 'empty if-body' warning. */ 322 | # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) 323 | # ifndef YYSTACK_ALLOC_MAXIMUM 324 | /* The OS might guarantee only one guard page at the bottom of the stack, 325 | and a page size can be as small as 4096 bytes. So we cannot safely 326 | invoke alloca (N) if N exceeds 4096. Use a slightly smaller number 327 | to allow for a few compiler-allocated temporary stack slots. */ 328 | # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ 329 | # endif 330 | # else 331 | # define YYSTACK_ALLOC YYMALLOC 332 | # define YYSTACK_FREE YYFREE 333 | # ifndef YYSTACK_ALLOC_MAXIMUM 334 | # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM 335 | # endif 336 | # if (defined __cplusplus && ! defined EXIT_SUCCESS \ 337 | && ! ((defined YYMALLOC || defined malloc) \ 338 | && (defined YYFREE || defined free))) 339 | # include /* INFRINGES ON USER NAME SPACE */ 340 | # ifndef EXIT_SUCCESS 341 | # define EXIT_SUCCESS 0 342 | # endif 343 | # endif 344 | # ifndef YYMALLOC 345 | # define YYMALLOC malloc 346 | # if ! defined malloc && ! defined EXIT_SUCCESS 347 | void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ 348 | # endif 349 | # endif 350 | # ifndef YYFREE 351 | # define YYFREE free 352 | # if ! defined free && ! defined EXIT_SUCCESS 353 | void free (void *); /* INFRINGES ON USER NAME SPACE */ 354 | # endif 355 | # endif 356 | # endif 357 | #endif /* ! defined yyoverflow || YYERROR_VERBOSE */ 358 | 359 | 360 | #if (! defined yyoverflow \ 361 | && (! defined __cplusplus \ 362 | || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) 363 | 364 | /* A type that is properly aligned for any stack member. */ 365 | union yyalloc 366 | { 367 | yytype_int16 yyss_alloc; 368 | YYSTYPE yyvs_alloc; 369 | }; 370 | 371 | /* The size of the maximum gap between one aligned stack and the next. */ 372 | # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) 373 | 374 | /* The size of an array large to enough to hold all stacks, each with 375 | N elements. */ 376 | # define YYSTACK_BYTES(N) \ 377 | ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \ 378 | + YYSTACK_GAP_MAXIMUM) 379 | 380 | # define YYCOPY_NEEDED 1 381 | 382 | /* Relocate STACK from its old location to the new one. The 383 | local variables YYSIZE and YYSTACKSIZE give the old and new number of 384 | elements in the stack, and YYPTR gives the new location of the 385 | stack. Advance YYPTR to a properly aligned location for the next 386 | stack. */ 387 | # define YYSTACK_RELOCATE(Stack_alloc, Stack) \ 388 | do \ 389 | { \ 390 | YYSIZE_T yynewbytes; \ 391 | YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ 392 | Stack = &yyptr->Stack_alloc; \ 393 | yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ 394 | yyptr += yynewbytes / sizeof (*yyptr); \ 395 | } \ 396 | while (0) 397 | 398 | #endif 399 | 400 | #if defined YYCOPY_NEEDED && YYCOPY_NEEDED 401 | /* Copy COUNT objects from SRC to DST. The source and destination do 402 | not overlap. */ 403 | # ifndef YYCOPY 404 | # if defined __GNUC__ && 1 < __GNUC__ 405 | # define YYCOPY(Dst, Src, Count) \ 406 | __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src))) 407 | # else 408 | # define YYCOPY(Dst, Src, Count) \ 409 | do \ 410 | { \ 411 | YYSIZE_T yyi; \ 412 | for (yyi = 0; yyi < (Count); yyi++) \ 413 | (Dst)[yyi] = (Src)[yyi]; \ 414 | } \ 415 | while (0) 416 | # endif 417 | # endif 418 | #endif /* !YYCOPY_NEEDED */ 419 | 420 | /* YYFINAL -- State number of the termination state. */ 421 | #define YYFINAL 8 422 | /* YYLAST -- Last index in YYTABLE. */ 423 | #define YYLAST 20 424 | 425 | /* YYNTOKENS -- Number of terminals. */ 426 | #define YYNTOKENS 32 427 | /* YYNNTS -- Number of nonterminals. */ 428 | #define YYNNTS 9 429 | /* YYNRULES -- Number of rules. */ 430 | #define YYNRULES 16 431 | /* YYNSTATES -- Number of states. */ 432 | #define YYNSTATES 30 433 | 434 | /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned 435 | by yylex, with out-of-bounds checking. */ 436 | #define YYUNDEFTOK 2 437 | #define YYMAXUTOK 286 438 | 439 | #define YYTRANSLATE(YYX) \ 440 | ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) 441 | 442 | /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM 443 | as returned by yylex, without out-of-bounds checking. */ 444 | static const yytype_uint8 yytranslate[] = 445 | { 446 | 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 447 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 448 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 449 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 450 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 451 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 452 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 453 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 454 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 455 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 456 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 457 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 458 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 459 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 460 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 461 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 462 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 463 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 464 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 465 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 466 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 467 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 468 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 469 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 470 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 471 | 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, 472 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 473 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 474 | 25, 26, 27, 28, 29, 30, 31 475 | }; 476 | 477 | #if YYDEBUG 478 | /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ 479 | static const yytype_uint8 yyrline[] = 480 | { 481 | 0, 19, 19, 22, 23, 26, 27, 28, 29, 32, 482 | 33, 36, 39, 42, 45, 46, 47 483 | }; 484 | #endif 485 | 486 | #if YYDEBUG || YYERROR_VERBOSE || 0 487 | /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. 488 | First, the terminals, then, starting at YYNTOKENS, nonterminals. */ 489 | static const char *const yytname[] = 490 | { 491 | "$end", "error", "$undefined", "IDENTIFIER", "NUM", "CHAR", "INT", 492 | "FLOAT", "DOUBLE", "EQ", "LE", "GE", "AND", "OR", "XOR", "ASSIGN", "IF", 493 | "ELSE", "SWITCH", "BREAK", "WHILE", "CASE", "ADD", "SUB", "MUL", "DIV", 494 | "SEMICOLON", "COMMA", "OP", "CP", "OB", "CB", "$accept", "program", 495 | "declarations", "type", "variables", "function", "argumentlist", 496 | "statements", "expressions", YY_NULLPTR 497 | }; 498 | #endif 499 | 500 | # ifdef YYPRINT 501 | /* YYTOKNUM[NUM] -- (External) token number corresponding to the 502 | (internal) symbol number NUM (which must be that of a token). */ 503 | static const yytype_uint16 yytoknum[] = 504 | { 505 | 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, 506 | 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 507 | 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 508 | 285, 286 509 | }; 510 | # endif 511 | 512 | #define YYPACT_NINF -17 513 | 514 | #define yypact_value_is_default(Yystate) \ 515 | (!!((Yystate) == (-17))) 516 | 517 | #define YYTABLE_NINF -1 518 | 519 | #define yytable_value_is_error(Yytable_value) \ 520 | 0 521 | 522 | /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing 523 | STATE-NUM. */ 524 | static const yytype_int8 yypact[] = 525 | { 526 | -5, -17, -17, -17, -17, 5, -5, 6, -17, 7, 527 | -17, -16, -14, -15, 6, -17, 6, -17, -17, -13, 528 | -12, 4, -1, -17, -11, -9, 4, -17, -17, -17 529 | }; 530 | 531 | /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. 532 | Performed when YYTABLE does not specify something else to do. Zero 533 | means the default is an error. */ 534 | static const yytype_uint8 yydefact[] = 535 | { 536 | 0, 6, 5, 7, 8, 0, 0, 0, 1, 0, 537 | 2, 9, 0, 0, 0, 3, 0, 10, 12, 0, 538 | 0, 0, 15, 16, 0, 0, 0, 11, 13, 14 539 | }; 540 | 541 | /* YYPGOTO[NTERM-NUM]. */ 542 | static const yytype_int8 yypgoto[] = 543 | { 544 | -17, -17, -17, 9, -10, -17, -17, -17, -7 545 | }; 546 | 547 | /* YYDEFGOTO[NTERM-NUM]. */ 548 | static const yytype_int8 yydefgoto[] = 549 | { 550 | -1, 5, 6, 7, 12, 10, 19, 24, 25 551 | }; 552 | 553 | /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If 554 | positive, shift that token. If negative, reduce the rule whose 555 | number is the opposite. If YYTABLE_NINF, syntax error. */ 556 | static const yytype_uint8 yytable[] = 557 | { 558 | 1, 2, 3, 4, 17, 8, 18, 22, 23, 11, 559 | 13, 14, 15, 16, 26, 9, 20, 28, 21, 29, 560 | 27 561 | }; 562 | 563 | static const yytype_uint8 yycheck[] = 564 | { 565 | 5, 6, 7, 8, 14, 0, 16, 3, 4, 3, 566 | 3, 27, 26, 28, 15, 6, 29, 26, 30, 26, 567 | 31 568 | }; 569 | 570 | /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing 571 | symbol of state STATE-NUM. */ 572 | static const yytype_uint8 yystos[] = 573 | { 574 | 0, 5, 6, 7, 8, 33, 34, 35, 0, 35, 575 | 37, 3, 36, 3, 27, 26, 28, 36, 36, 38, 576 | 29, 30, 3, 4, 39, 40, 15, 31, 26, 40 577 | }; 578 | 579 | /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ 580 | static const yytype_uint8 yyr1[] = 581 | { 582 | 0, 32, 33, 34, 34, 35, 35, 35, 35, 36, 583 | 36, 37, 38, 39, 40, 40, 40 584 | }; 585 | 586 | /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ 587 | static const yytype_uint8 yyr2[] = 588 | { 589 | 0, 2, 2, 3, 0, 1, 1, 1, 1, 1, 590 | 3, 8, 1, 2, 3, 1, 1 591 | }; 592 | 593 | 594 | #define yyerrok (yyerrstatus = 0) 595 | #define yyclearin (yychar = YYEMPTY) 596 | #define YYEMPTY (-2) 597 | #define YYEOF 0 598 | 599 | #define YYACCEPT goto yyacceptlab 600 | #define YYABORT goto yyabortlab 601 | #define YYERROR goto yyerrorlab 602 | 603 | 604 | #define YYRECOVERING() (!!yyerrstatus) 605 | 606 | #define YYBACKUP(Token, Value) \ 607 | do \ 608 | if (yychar == YYEMPTY) \ 609 | { \ 610 | yychar = (Token); \ 611 | yylval = (Value); \ 612 | YYPOPSTACK (yylen); \ 613 | yystate = *yyssp; \ 614 | goto yybackup; \ 615 | } \ 616 | else \ 617 | { \ 618 | yyerror (YY_("syntax error: cannot back up")); \ 619 | YYERROR; \ 620 | } \ 621 | while (0) 622 | 623 | /* Error token number */ 624 | #define YYTERROR 1 625 | #define YYERRCODE 256 626 | 627 | 628 | 629 | /* Enable debugging if requested. */ 630 | #if YYDEBUG 631 | 632 | # ifndef YYFPRINTF 633 | # include /* INFRINGES ON USER NAME SPACE */ 634 | # define YYFPRINTF fprintf 635 | # endif 636 | 637 | # define YYDPRINTF(Args) \ 638 | do { \ 639 | if (yydebug) \ 640 | YYFPRINTF Args; \ 641 | } while (0) 642 | 643 | /* This macro is provided for backward compatibility. */ 644 | #ifndef YY_LOCATION_PRINT 645 | # define YY_LOCATION_PRINT(File, Loc) ((void) 0) 646 | #endif 647 | 648 | 649 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ 650 | do { \ 651 | if (yydebug) \ 652 | { \ 653 | YYFPRINTF (stderr, "%s ", Title); \ 654 | yy_symbol_print (stderr, \ 655 | Type, Value); \ 656 | YYFPRINTF (stderr, "\n"); \ 657 | } \ 658 | } while (0) 659 | 660 | 661 | /*----------------------------------------. 662 | | Print this symbol's value on YYOUTPUT. | 663 | `----------------------------------------*/ 664 | 665 | static void 666 | yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) 667 | { 668 | FILE *yyo = yyoutput; 669 | YYUSE (yyo); 670 | if (!yyvaluep) 671 | return; 672 | # ifdef YYPRINT 673 | if (yytype < YYNTOKENS) 674 | YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); 675 | # endif 676 | YYUSE (yytype); 677 | } 678 | 679 | 680 | /*--------------------------------. 681 | | Print this symbol on YYOUTPUT. | 682 | `--------------------------------*/ 683 | 684 | static void 685 | yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) 686 | { 687 | YYFPRINTF (yyoutput, "%s %s (", 688 | yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); 689 | 690 | yy_symbol_value_print (yyoutput, yytype, yyvaluep); 691 | YYFPRINTF (yyoutput, ")"); 692 | } 693 | 694 | /*------------------------------------------------------------------. 695 | | yy_stack_print -- Print the state stack from its BOTTOM up to its | 696 | | TOP (included). | 697 | `------------------------------------------------------------------*/ 698 | 699 | static void 700 | yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop) 701 | { 702 | YYFPRINTF (stderr, "Stack now"); 703 | for (; yybottom <= yytop; yybottom++) 704 | { 705 | int yybot = *yybottom; 706 | YYFPRINTF (stderr, " %d", yybot); 707 | } 708 | YYFPRINTF (stderr, "\n"); 709 | } 710 | 711 | # define YY_STACK_PRINT(Bottom, Top) \ 712 | do { \ 713 | if (yydebug) \ 714 | yy_stack_print ((Bottom), (Top)); \ 715 | } while (0) 716 | 717 | 718 | /*------------------------------------------------. 719 | | Report that the YYRULE is going to be reduced. | 720 | `------------------------------------------------*/ 721 | 722 | static void 723 | yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule) 724 | { 725 | unsigned long int yylno = yyrline[yyrule]; 726 | int yynrhs = yyr2[yyrule]; 727 | int yyi; 728 | YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n", 729 | yyrule - 1, yylno); 730 | /* The symbols being reduced. */ 731 | for (yyi = 0; yyi < yynrhs; yyi++) 732 | { 733 | YYFPRINTF (stderr, " $%d = ", yyi + 1); 734 | yy_symbol_print (stderr, 735 | yystos[yyssp[yyi + 1 - yynrhs]], 736 | &(yyvsp[(yyi + 1) - (yynrhs)]) 737 | ); 738 | YYFPRINTF (stderr, "\n"); 739 | } 740 | } 741 | 742 | # define YY_REDUCE_PRINT(Rule) \ 743 | do { \ 744 | if (yydebug) \ 745 | yy_reduce_print (yyssp, yyvsp, Rule); \ 746 | } while (0) 747 | 748 | /* Nonzero means print parse trace. It is left uninitialized so that 749 | multiple parsers can coexist. */ 750 | int yydebug; 751 | #else /* !YYDEBUG */ 752 | # define YYDPRINTF(Args) 753 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) 754 | # define YY_STACK_PRINT(Bottom, Top) 755 | # define YY_REDUCE_PRINT(Rule) 756 | #endif /* !YYDEBUG */ 757 | 758 | 759 | /* YYINITDEPTH -- initial size of the parser's stacks. */ 760 | #ifndef YYINITDEPTH 761 | # define YYINITDEPTH 200 762 | #endif 763 | 764 | /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only 765 | if the built-in stack extension method is used). 766 | 767 | Do not make this value too large; the results are undefined if 768 | YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) 769 | evaluated with infinite-precision integer arithmetic. */ 770 | 771 | #ifndef YYMAXDEPTH 772 | # define YYMAXDEPTH 10000 773 | #endif 774 | 775 | 776 | #if YYERROR_VERBOSE 777 | 778 | # ifndef yystrlen 779 | # if defined __GLIBC__ && defined _STRING_H 780 | # define yystrlen strlen 781 | # else 782 | /* Return the length of YYSTR. */ 783 | static YYSIZE_T 784 | yystrlen (const char *yystr) 785 | { 786 | YYSIZE_T yylen; 787 | for (yylen = 0; yystr[yylen]; yylen++) 788 | continue; 789 | return yylen; 790 | } 791 | # endif 792 | # endif 793 | 794 | # ifndef yystpcpy 795 | # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE 796 | # define yystpcpy stpcpy 797 | # else 798 | /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in 799 | YYDEST. */ 800 | static char * 801 | yystpcpy (char *yydest, const char *yysrc) 802 | { 803 | char *yyd = yydest; 804 | const char *yys = yysrc; 805 | 806 | while ((*yyd++ = *yys++) != '\0') 807 | continue; 808 | 809 | return yyd - 1; 810 | } 811 | # endif 812 | # endif 813 | 814 | # ifndef yytnamerr 815 | /* Copy to YYRES the contents of YYSTR after stripping away unnecessary 816 | quotes and backslashes, so that it's suitable for yyerror. The 817 | heuristic is that double-quoting is unnecessary unless the string 818 | contains an apostrophe, a comma, or backslash (other than 819 | backslash-backslash). YYSTR is taken from yytname. If YYRES is 820 | null, do not copy; instead, return the length of what the result 821 | would have been. */ 822 | static YYSIZE_T 823 | yytnamerr (char *yyres, const char *yystr) 824 | { 825 | if (*yystr == '"') 826 | { 827 | YYSIZE_T yyn = 0; 828 | char const *yyp = yystr; 829 | 830 | for (;;) 831 | switch (*++yyp) 832 | { 833 | case '\'': 834 | case ',': 835 | goto do_not_strip_quotes; 836 | 837 | case '\\': 838 | if (*++yyp != '\\') 839 | goto do_not_strip_quotes; 840 | /* Fall through. */ 841 | default: 842 | if (yyres) 843 | yyres[yyn] = *yyp; 844 | yyn++; 845 | break; 846 | 847 | case '"': 848 | if (yyres) 849 | yyres[yyn] = '\0'; 850 | return yyn; 851 | } 852 | do_not_strip_quotes: ; 853 | } 854 | 855 | if (! yyres) 856 | return yystrlen (yystr); 857 | 858 | return yystpcpy (yyres, yystr) - yyres; 859 | } 860 | # endif 861 | 862 | /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message 863 | about the unexpected token YYTOKEN for the state stack whose top is 864 | YYSSP. 865 | 866 | Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is 867 | not large enough to hold the message. In that case, also set 868 | *YYMSG_ALLOC to the required number of bytes. Return 2 if the 869 | required number of bytes is too large to store. */ 870 | static int 871 | yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, 872 | yytype_int16 *yyssp, int yytoken) 873 | { 874 | YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); 875 | YYSIZE_T yysize = yysize0; 876 | enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; 877 | /* Internationalized format string. */ 878 | const char *yyformat = YY_NULLPTR; 879 | /* Arguments of yyformat. */ 880 | char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; 881 | /* Number of reported tokens (one for the "unexpected", one per 882 | "expected"). */ 883 | int yycount = 0; 884 | 885 | /* There are many possibilities here to consider: 886 | - If this state is a consistent state with a default action, then 887 | the only way this function was invoked is if the default action 888 | is an error action. In that case, don't check for expected 889 | tokens because there are none. 890 | - The only way there can be no lookahead present (in yychar) is if 891 | this state is a consistent state with a default action. Thus, 892 | detecting the absence of a lookahead is sufficient to determine 893 | that there is no unexpected or expected token to report. In that 894 | case, just report a simple "syntax error". 895 | - Don't assume there isn't a lookahead just because this state is a 896 | consistent state with a default action. There might have been a 897 | previous inconsistent state, consistent state with a non-default 898 | action, or user semantic action that manipulated yychar. 899 | - Of course, the expected token list depends on states to have 900 | correct lookahead information, and it depends on the parser not 901 | to perform extra reductions after fetching a lookahead from the 902 | scanner and before detecting a syntax error. Thus, state merging 903 | (from LALR or IELR) and default reductions corrupt the expected 904 | token list. However, the list is correct for canonical LR with 905 | one exception: it will still contain any token that will not be 906 | accepted due to an error action in a later state. 907 | */ 908 | if (yytoken != YYEMPTY) 909 | { 910 | int yyn = yypact[*yyssp]; 911 | yyarg[yycount++] = yytname[yytoken]; 912 | if (!yypact_value_is_default (yyn)) 913 | { 914 | /* Start YYX at -YYN if negative to avoid negative indexes in 915 | YYCHECK. In other words, skip the first -YYN actions for 916 | this state because they are default actions. */ 917 | int yyxbegin = yyn < 0 ? -yyn : 0; 918 | /* Stay within bounds of both yycheck and yytname. */ 919 | int yychecklim = YYLAST - yyn + 1; 920 | int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; 921 | int yyx; 922 | 923 | for (yyx = yyxbegin; yyx < yyxend; ++yyx) 924 | if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR 925 | && !yytable_value_is_error (yytable[yyx + yyn])) 926 | { 927 | if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) 928 | { 929 | yycount = 1; 930 | yysize = yysize0; 931 | break; 932 | } 933 | yyarg[yycount++] = yytname[yyx]; 934 | { 935 | YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); 936 | if (! (yysize <= yysize1 937 | && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) 938 | return 2; 939 | yysize = yysize1; 940 | } 941 | } 942 | } 943 | } 944 | 945 | switch (yycount) 946 | { 947 | # define YYCASE_(N, S) \ 948 | case N: \ 949 | yyformat = S; \ 950 | break 951 | YYCASE_(0, YY_("syntax error")); 952 | YYCASE_(1, YY_("syntax error, unexpected %s")); 953 | YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); 954 | YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); 955 | YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); 956 | YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); 957 | # undef YYCASE_ 958 | } 959 | 960 | { 961 | YYSIZE_T yysize1 = yysize + yystrlen (yyformat); 962 | if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) 963 | return 2; 964 | yysize = yysize1; 965 | } 966 | 967 | if (*yymsg_alloc < yysize) 968 | { 969 | *yymsg_alloc = 2 * yysize; 970 | if (! (yysize <= *yymsg_alloc 971 | && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) 972 | *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; 973 | return 1; 974 | } 975 | 976 | /* Avoid sprintf, as that infringes on the user's name space. 977 | Don't have undefined behavior even if the translation 978 | produced a string with the wrong number of "%s"s. */ 979 | { 980 | char *yyp = *yymsg; 981 | int yyi = 0; 982 | while ((*yyp = *yyformat) != '\0') 983 | if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) 984 | { 985 | yyp += yytnamerr (yyp, yyarg[yyi++]); 986 | yyformat += 2; 987 | } 988 | else 989 | { 990 | yyp++; 991 | yyformat++; 992 | } 993 | } 994 | return 0; 995 | } 996 | #endif /* YYERROR_VERBOSE */ 997 | 998 | /*-----------------------------------------------. 999 | | Release the memory associated to this symbol. | 1000 | `-----------------------------------------------*/ 1001 | 1002 | static void 1003 | yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep) 1004 | { 1005 | YYUSE (yyvaluep); 1006 | if (!yymsg) 1007 | yymsg = "Deleting"; 1008 | YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); 1009 | 1010 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1011 | YYUSE (yytype); 1012 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1013 | } 1014 | 1015 | 1016 | 1017 | 1018 | /* The lookahead symbol. */ 1019 | int yychar; 1020 | 1021 | /* The semantic value of the lookahead symbol. */ 1022 | YYSTYPE yylval; 1023 | /* Number of syntax errors so far. */ 1024 | int yynerrs; 1025 | 1026 | 1027 | /*----------. 1028 | | yyparse. | 1029 | `----------*/ 1030 | 1031 | int 1032 | yyparse (void) 1033 | { 1034 | int yystate; 1035 | /* Number of tokens to shift before error messages enabled. */ 1036 | int yyerrstatus; 1037 | 1038 | /* The stacks and their tools: 1039 | 'yyss': related to states. 1040 | 'yyvs': related to semantic values. 1041 | 1042 | Refer to the stacks through separate pointers, to allow yyoverflow 1043 | to reallocate them elsewhere. */ 1044 | 1045 | /* The state stack. */ 1046 | yytype_int16 yyssa[YYINITDEPTH]; 1047 | yytype_int16 *yyss; 1048 | yytype_int16 *yyssp; 1049 | 1050 | /* The semantic value stack. */ 1051 | YYSTYPE yyvsa[YYINITDEPTH]; 1052 | YYSTYPE *yyvs; 1053 | YYSTYPE *yyvsp; 1054 | 1055 | YYSIZE_T yystacksize; 1056 | 1057 | int yyn; 1058 | int yyresult; 1059 | /* Lookahead token as an internal (translated) token number. */ 1060 | int yytoken = 0; 1061 | /* The variables used to return semantic value and location from the 1062 | action routines. */ 1063 | YYSTYPE yyval; 1064 | 1065 | #if YYERROR_VERBOSE 1066 | /* Buffer for error messages, and its allocated size. */ 1067 | char yymsgbuf[128]; 1068 | char *yymsg = yymsgbuf; 1069 | YYSIZE_T yymsg_alloc = sizeof yymsgbuf; 1070 | #endif 1071 | 1072 | #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) 1073 | 1074 | /* The number of symbols on the RHS of the reduced rule. 1075 | Keep to zero when no symbol should be popped. */ 1076 | int yylen = 0; 1077 | 1078 | yyssp = yyss = yyssa; 1079 | yyvsp = yyvs = yyvsa; 1080 | yystacksize = YYINITDEPTH; 1081 | 1082 | YYDPRINTF ((stderr, "Starting parse\n")); 1083 | 1084 | yystate = 0; 1085 | yyerrstatus = 0; 1086 | yynerrs = 0; 1087 | yychar = YYEMPTY; /* Cause a token to be read. */ 1088 | goto yysetstate; 1089 | 1090 | /*------------------------------------------------------------. 1091 | | yynewstate -- Push a new state, which is found in yystate. | 1092 | `------------------------------------------------------------*/ 1093 | yynewstate: 1094 | /* In all cases, when you get here, the value and location stacks 1095 | have just been pushed. So pushing a state here evens the stacks. */ 1096 | yyssp++; 1097 | 1098 | yysetstate: 1099 | *yyssp = yystate; 1100 | 1101 | if (yyss + yystacksize - 1 <= yyssp) 1102 | { 1103 | /* Get the current used size of the three stacks, in elements. */ 1104 | YYSIZE_T yysize = yyssp - yyss + 1; 1105 | 1106 | #ifdef yyoverflow 1107 | { 1108 | /* Give user a chance to reallocate the stack. Use copies of 1109 | these so that the &'s don't force the real ones into 1110 | memory. */ 1111 | YYSTYPE *yyvs1 = yyvs; 1112 | yytype_int16 *yyss1 = yyss; 1113 | 1114 | /* Each stack pointer address is followed by the size of the 1115 | data in use in that stack, in bytes. This used to be a 1116 | conditional around just the two extra args, but that might 1117 | be undefined if yyoverflow is a macro. */ 1118 | yyoverflow (YY_("memory exhausted"), 1119 | &yyss1, yysize * sizeof (*yyssp), 1120 | &yyvs1, yysize * sizeof (*yyvsp), 1121 | &yystacksize); 1122 | 1123 | yyss = yyss1; 1124 | yyvs = yyvs1; 1125 | } 1126 | #else /* no yyoverflow */ 1127 | # ifndef YYSTACK_RELOCATE 1128 | goto yyexhaustedlab; 1129 | # else 1130 | /* Extend the stack our own way. */ 1131 | if (YYMAXDEPTH <= yystacksize) 1132 | goto yyexhaustedlab; 1133 | yystacksize *= 2; 1134 | if (YYMAXDEPTH < yystacksize) 1135 | yystacksize = YYMAXDEPTH; 1136 | 1137 | { 1138 | yytype_int16 *yyss1 = yyss; 1139 | union yyalloc *yyptr = 1140 | (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); 1141 | if (! yyptr) 1142 | goto yyexhaustedlab; 1143 | YYSTACK_RELOCATE (yyss_alloc, yyss); 1144 | YYSTACK_RELOCATE (yyvs_alloc, yyvs); 1145 | # undef YYSTACK_RELOCATE 1146 | if (yyss1 != yyssa) 1147 | YYSTACK_FREE (yyss1); 1148 | } 1149 | # endif 1150 | #endif /* no yyoverflow */ 1151 | 1152 | yyssp = yyss + yysize - 1; 1153 | yyvsp = yyvs + yysize - 1; 1154 | 1155 | YYDPRINTF ((stderr, "Stack size increased to %lu\n", 1156 | (unsigned long int) yystacksize)); 1157 | 1158 | if (yyss + yystacksize - 1 <= yyssp) 1159 | YYABORT; 1160 | } 1161 | 1162 | YYDPRINTF ((stderr, "Entering state %d\n", yystate)); 1163 | 1164 | if (yystate == YYFINAL) 1165 | YYACCEPT; 1166 | 1167 | goto yybackup; 1168 | 1169 | /*-----------. 1170 | | yybackup. | 1171 | `-----------*/ 1172 | yybackup: 1173 | 1174 | /* Do appropriate processing given the current state. Read a 1175 | lookahead token if we need one and don't already have one. */ 1176 | 1177 | /* First try to decide what to do without reference to lookahead token. */ 1178 | yyn = yypact[yystate]; 1179 | if (yypact_value_is_default (yyn)) 1180 | goto yydefault; 1181 | 1182 | /* Not known => get a lookahead token if don't already have one. */ 1183 | 1184 | /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ 1185 | if (yychar == YYEMPTY) 1186 | { 1187 | YYDPRINTF ((stderr, "Reading a token: ")); 1188 | yychar = yylex (); 1189 | } 1190 | 1191 | if (yychar <= YYEOF) 1192 | { 1193 | yychar = yytoken = YYEOF; 1194 | YYDPRINTF ((stderr, "Now at end of input.\n")); 1195 | } 1196 | else 1197 | { 1198 | yytoken = YYTRANSLATE (yychar); 1199 | YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); 1200 | } 1201 | 1202 | /* If the proper action on seeing token YYTOKEN is to reduce or to 1203 | detect an error, take that action. */ 1204 | yyn += yytoken; 1205 | if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) 1206 | goto yydefault; 1207 | yyn = yytable[yyn]; 1208 | if (yyn <= 0) 1209 | { 1210 | if (yytable_value_is_error (yyn)) 1211 | goto yyerrlab; 1212 | yyn = -yyn; 1213 | goto yyreduce; 1214 | } 1215 | 1216 | /* Count tokens shifted since error; after three, turn off error 1217 | status. */ 1218 | if (yyerrstatus) 1219 | yyerrstatus--; 1220 | 1221 | /* Shift the lookahead token. */ 1222 | YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); 1223 | 1224 | /* Discard the shifted token. */ 1225 | yychar = YYEMPTY; 1226 | 1227 | yystate = yyn; 1228 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1229 | *++yyvsp = yylval; 1230 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1231 | 1232 | goto yynewstate; 1233 | 1234 | 1235 | /*-----------------------------------------------------------. 1236 | | yydefault -- do the default action for the current state. | 1237 | `-----------------------------------------------------------*/ 1238 | yydefault: 1239 | yyn = yydefact[yystate]; 1240 | if (yyn == 0) 1241 | goto yyerrlab; 1242 | goto yyreduce; 1243 | 1244 | 1245 | /*-----------------------------. 1246 | | yyreduce -- Do a reduction. | 1247 | `-----------------------------*/ 1248 | yyreduce: 1249 | /* yyn is the number of a rule to reduce with. */ 1250 | yylen = yyr2[yyn]; 1251 | 1252 | /* If YYLEN is nonzero, implement the default value of the action: 1253 | '$$ = $1'. 1254 | 1255 | Otherwise, the following line sets YYVAL to garbage. 1256 | This behavior is undocumented and Bison 1257 | users should not rely upon it. Assigning to YYVAL 1258 | unconditionally makes the parser a bit smaller, and it avoids a 1259 | GCC warning that YYVAL may be used uninitialized. */ 1260 | yyval = yyvsp[1-yylen]; 1261 | 1262 | 1263 | YY_REDUCE_PRINT (yyn); 1264 | switch (yyn) 1265 | { 1266 | case 2: 1267 | #line 19 "prog.y" /* yacc.c:1646 */ 1268 | {printf("Input accepted\n");exit(0);} 1269 | #line 1270 "y.tab.c" /* yacc.c:1646 */ 1270 | break; 1271 | 1272 | 1273 | #line 1274 "y.tab.c" /* yacc.c:1646 */ 1274 | default: break; 1275 | } 1276 | /* User semantic actions sometimes alter yychar, and that requires 1277 | that yytoken be updated with the new translation. We take the 1278 | approach of translating immediately before every use of yytoken. 1279 | One alternative is translating here after every semantic action, 1280 | but that translation would be missed if the semantic action invokes 1281 | YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or 1282 | if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an 1283 | incorrect destructor might then be invoked immediately. In the 1284 | case of YYERROR or YYBACKUP, subsequent parser actions might lead 1285 | to an incorrect destructor call or verbose syntax error message 1286 | before the lookahead is translated. */ 1287 | YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); 1288 | 1289 | YYPOPSTACK (yylen); 1290 | yylen = 0; 1291 | YY_STACK_PRINT (yyss, yyssp); 1292 | 1293 | *++yyvsp = yyval; 1294 | 1295 | /* Now 'shift' the result of the reduction. Determine what state 1296 | that goes to, based on the state we popped back to and the rule 1297 | number reduced by. */ 1298 | 1299 | yyn = yyr1[yyn]; 1300 | 1301 | yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; 1302 | if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) 1303 | yystate = yytable[yystate]; 1304 | else 1305 | yystate = yydefgoto[yyn - YYNTOKENS]; 1306 | 1307 | goto yynewstate; 1308 | 1309 | 1310 | /*--------------------------------------. 1311 | | yyerrlab -- here on detecting error. | 1312 | `--------------------------------------*/ 1313 | yyerrlab: 1314 | /* Make sure we have latest lookahead translation. See comments at 1315 | user semantic actions for why this is necessary. */ 1316 | yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); 1317 | 1318 | /* If not already recovering from an error, report this error. */ 1319 | if (!yyerrstatus) 1320 | { 1321 | ++yynerrs; 1322 | #if ! YYERROR_VERBOSE 1323 | yyerror (YY_("syntax error")); 1324 | #else 1325 | # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ 1326 | yyssp, yytoken) 1327 | { 1328 | char const *yymsgp = YY_("syntax error"); 1329 | int yysyntax_error_status; 1330 | yysyntax_error_status = YYSYNTAX_ERROR; 1331 | if (yysyntax_error_status == 0) 1332 | yymsgp = yymsg; 1333 | else if (yysyntax_error_status == 1) 1334 | { 1335 | if (yymsg != yymsgbuf) 1336 | YYSTACK_FREE (yymsg); 1337 | yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc); 1338 | if (!yymsg) 1339 | { 1340 | yymsg = yymsgbuf; 1341 | yymsg_alloc = sizeof yymsgbuf; 1342 | yysyntax_error_status = 2; 1343 | } 1344 | else 1345 | { 1346 | yysyntax_error_status = YYSYNTAX_ERROR; 1347 | yymsgp = yymsg; 1348 | } 1349 | } 1350 | yyerror (yymsgp); 1351 | if (yysyntax_error_status == 2) 1352 | goto yyexhaustedlab; 1353 | } 1354 | # undef YYSYNTAX_ERROR 1355 | #endif 1356 | } 1357 | 1358 | 1359 | 1360 | if (yyerrstatus == 3) 1361 | { 1362 | /* If just tried and failed to reuse lookahead token after an 1363 | error, discard it. */ 1364 | 1365 | if (yychar <= YYEOF) 1366 | { 1367 | /* Return failure if at end of input. */ 1368 | if (yychar == YYEOF) 1369 | YYABORT; 1370 | } 1371 | else 1372 | { 1373 | yydestruct ("Error: discarding", 1374 | yytoken, &yylval); 1375 | yychar = YYEMPTY; 1376 | } 1377 | } 1378 | 1379 | /* Else will try to reuse lookahead token after shifting the error 1380 | token. */ 1381 | goto yyerrlab1; 1382 | 1383 | 1384 | /*---------------------------------------------------. 1385 | | yyerrorlab -- error raised explicitly by YYERROR. | 1386 | `---------------------------------------------------*/ 1387 | yyerrorlab: 1388 | 1389 | /* Pacify compilers like GCC when the user code never invokes 1390 | YYERROR and the label yyerrorlab therefore never appears in user 1391 | code. */ 1392 | if (/*CONSTCOND*/ 0) 1393 | goto yyerrorlab; 1394 | 1395 | /* Do not reclaim the symbols of the rule whose action triggered 1396 | this YYERROR. */ 1397 | YYPOPSTACK (yylen); 1398 | yylen = 0; 1399 | YY_STACK_PRINT (yyss, yyssp); 1400 | yystate = *yyssp; 1401 | goto yyerrlab1; 1402 | 1403 | 1404 | /*-------------------------------------------------------------. 1405 | | yyerrlab1 -- common code for both syntax error and YYERROR. | 1406 | `-------------------------------------------------------------*/ 1407 | yyerrlab1: 1408 | yyerrstatus = 3; /* Each real token shifted decrements this. */ 1409 | 1410 | for (;;) 1411 | { 1412 | yyn = yypact[yystate]; 1413 | if (!yypact_value_is_default (yyn)) 1414 | { 1415 | yyn += YYTERROR; 1416 | if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) 1417 | { 1418 | yyn = yytable[yyn]; 1419 | if (0 < yyn) 1420 | break; 1421 | } 1422 | } 1423 | 1424 | /* Pop the current state because it cannot handle the error token. */ 1425 | if (yyssp == yyss) 1426 | YYABORT; 1427 | 1428 | 1429 | yydestruct ("Error: popping", 1430 | yystos[yystate], yyvsp); 1431 | YYPOPSTACK (1); 1432 | yystate = *yyssp; 1433 | YY_STACK_PRINT (yyss, yyssp); 1434 | } 1435 | 1436 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN 1437 | *++yyvsp = yylval; 1438 | YY_IGNORE_MAYBE_UNINITIALIZED_END 1439 | 1440 | 1441 | /* Shift the error token. */ 1442 | YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp); 1443 | 1444 | yystate = yyn; 1445 | goto yynewstate; 1446 | 1447 | 1448 | /*-------------------------------------. 1449 | | yyacceptlab -- YYACCEPT comes here. | 1450 | `-------------------------------------*/ 1451 | yyacceptlab: 1452 | yyresult = 0; 1453 | goto yyreturn; 1454 | 1455 | /*-----------------------------------. 1456 | | yyabortlab -- YYABORT comes here. | 1457 | `-----------------------------------*/ 1458 | yyabortlab: 1459 | yyresult = 1; 1460 | goto yyreturn; 1461 | 1462 | #if !defined yyoverflow || YYERROR_VERBOSE 1463 | /*-------------------------------------------------. 1464 | | yyexhaustedlab -- memory exhaustion comes here. | 1465 | `-------------------------------------------------*/ 1466 | yyexhaustedlab: 1467 | yyerror (YY_("memory exhausted")); 1468 | yyresult = 2; 1469 | /* Fall through. */ 1470 | #endif 1471 | 1472 | yyreturn: 1473 | if (yychar != YYEMPTY) 1474 | { 1475 | /* Make sure we have latest lookahead translation. See comments at 1476 | user semantic actions for why this is necessary. */ 1477 | yytoken = YYTRANSLATE (yychar); 1478 | yydestruct ("Cleanup: discarding lookahead", 1479 | yytoken, &yylval); 1480 | } 1481 | /* Do not reclaim the symbols of the rule whose action triggered 1482 | this YYABORT or YYACCEPT. */ 1483 | YYPOPSTACK (yylen); 1484 | YY_STACK_PRINT (yyss, yyssp); 1485 | while (yyssp != yyss) 1486 | { 1487 | yydestruct ("Cleanup: popping", 1488 | yystos[*yyssp], yyvsp); 1489 | YYPOPSTACK (1); 1490 | } 1491 | #ifndef yyoverflow 1492 | if (yyss != yyssa) 1493 | YYSTACK_FREE (yyss); 1494 | #endif 1495 | #if YYERROR_VERBOSE 1496 | if (yymsg != yymsgbuf) 1497 | YYSTACK_FREE (yymsg); 1498 | #endif 1499 | return yyresult; 1500 | } 1501 | #line 49 "prog.y" /* yacc.c:1906 */ 1502 | 1503 | 1504 | #include "lex.yy.c" 1505 | int main() 1506 | { 1507 | yyin=fopen("input.c","r"); 1508 | yyparse(); 1509 | fclose(yyin); 1510 | return 0; 1511 | } 1512 | 1513 | 1514 | 1515 | 1516 | -------------------------------------------------------------------------------- /readme: -------------------------------------------------------------------------------- 1 | Simple C Compiler 2 | 3 | The four main stages in compiler design : 4 | Lexical Analysis - The input program is broken down into tokens (identifiers, operators, numbers, keywords, punctuators). A symbol table is created with the list of tokens obtained from the input. Lexical Analysis is done using Flex tool to generate tokens. 5 | To install Lex : 6 | sudo apt-get install flex 7 | Execution : 8 | lex prog.l 9 | cc lex.yy.c -ll 10 | ./a.out 11 | cat output.txt 12 | 13 | Syntax Analysis - In this step, the tokens generated after lexical analysis is examined to construct syntactically correct sentences and the syntax errors present are printed out (missing parenthesis, semicolon). Syntax Analysis is performed using Yacc tool as a parser. 14 | To install Yacc : 15 | sudo apt-get install bison 16 | sudo apt-get install byacc 17 | Execution : 18 | lex prog.l 19 | yacc prog.y 20 | cc y.tab.c -ll -ly 21 | ./a.out 22 | 23 | Semantic Analysis - The parsed output generated in the above step is examined for semantic errors and the abstract syntax tree is printed. Semantic errors like type checking, undeclared variables, multiple declarations of variables are verified. 24 | Execution : 25 | lex prog.l 26 | yacc prog.y 27 | cc y.tab.c -ll -ly 28 | ./a.out 29 | cat out.txt 30 | 31 | lex prog.l 32 | yacc absprog.y 33 | cc y.tab.c -ll -ly 34 | ./a.out 35 | cat output.txt 36 | 37 | Code Generation - Using the abstract syntax tree we need to generate the intermediate code in three address code format. --------------------------------------------------------------------------------