parser/c_compiler/lexer.l
changeset 39 46d7ec9d63bd
parent 37 0fbbe329c3a2
equal deleted inserted replaced
38:48f6f3918b82 39:46d7ec9d63bd
     1 %{
     1 %{
     2 	#include <prototypes.h>
     2     #include <prototypes.h>
     3 	#include "parser.h"
     3     #include "parser.h"
     4 	int column = 0;
     4     int column = 0;
     5 	int c;
     5     int c;
     6 %}
     6 %}
     7 
     7 
     8 D						[0-9]
     8 D                        [0-9]
     9 L						[a-zA-Z_]
     9 L                        [a-zA-Z_]
    10 H						[a-fA-F0-9]
    10 H                        [a-fA-F0-9]
    11 E						[Ee][+-]?{D}+
    11 E                        [Ee][+-]?{D}+
    12 FS						(f|F|l|L)
    12 FS                       (f|F|l|L)
    13 IS						(u|U|l|L)*
    13 IS                       (u|U|l|L)*
    14 
    14 
    15 %%
    15 %%
    16 "/*"           			{ comment(); }
    16 "/*"                     { comment(); }
    17 "#include <"			{ c = input(); while ( c != '>') c = input();  }
    17 "#include <"             { c = input(); while ( c != '>') c = input();  }
    18 "#include \""			{ c = input(); while ( c != '\"') c = input(); }
    18 "#include \""            { c = input(); while ( c != '\"') c = input(); }
    19 
    19 
    20 "auto"					{ count(); return(AUTO); }
    20 "auto"                   { count(); return(AUTO); }
    21 "break"					{ count(); return(BREAK); }
    21 "break"                  { count(); return(BREAK); }
    22 "case"					{ count(); return(CASE); }
    22 "case"                   { count(); return(CASE); }
    23 "char"					{ count(); return(CHAR); }
    23 "char"                   { count(); return(CHAR); }
    24 "const"					{ count(); return(CONST); }
    24 "const"                  { count(); return(CONST); }
    25 "continue"				{ count(); return(CONTINUE); }
    25 "continue"               { count(); return(CONTINUE); }
    26 "default"				{ count(); return(DEFAULT); }
    26 "default"                { count(); return(DEFAULT); }
    27 "do"					{ count(); return(DO); }
    27 "do"                     { count(); return(DO); }
    28 "double"				{ count(); return(DOUBLE); }
    28 "double"                 { count(); return(DOUBLE); }
    29 "else"					{ count(); return(ELSE); }
    29 "else"                   { count(); return(ELSE); }
    30 "enum"					{ count(); return(ENUM); }
    30 "enum"                   { count(); return(ENUM); }
    31 "extern"				{ count(); return(EXTERN); }
    31 "extern"                 { count(); return(EXTERN); }
    32 "float"					{ count(); return(FLOAT); }
    32 "float"                  { count(); return(FLOAT); }
    33 "for"					{ count(); return(FOR); }
    33 "for"                    { count(); return(FOR); }
    34 "goto"					{ count(); return(GOTO); }
    34 "goto"                   { count(); return(GOTO); }
    35 "if"					{ count(); return(IF); }
    35 "if"                     { count(); return(IF); }
    36 "int"					{ count(); return(INT); }
    36 "int"                    { count(); return(INT); }
    37 "long"					{ count(); return(LONG); }
    37 "long"                   { count(); return(LONG); }
    38 "register"				{ count(); return(REGISTER); }
    38 "register"               { count(); return(REGISTER); }
    39 "return"				{ count(); return(RETURN); }
    39 "return"                 { count(); return(RETURN); }
    40 "short"					{ count(); return(SHORT); }
    40 "short"                  { count(); return(SHORT); }
    41 "signed"					{ count(); return(SIGNED); }
    41 "signed"                 { count(); return(SIGNED); }
    42 "sizeof"				{ count(); return(SIZEOF); }
    42 "sizeof"                 { count(); return(SIZEOF); }
    43 "static"				{ count(); return(STATIC); }
    43 "static"                 { count(); return(STATIC); }
    44 "struct"				{ count(); return(STRUCT); }
    44 "struct"                 { count(); return(STRUCT); }
    45 "switch"				{ count(); return(SWITCH); }
    45 "switch"                 { count(); return(SWITCH); }
    46 "typedef"				{ count(); return(TYPEDEF); }
    46 "typedef"                { count(); return(TYPEDEF); }
    47 "union"					{ count(); return(UNION); }
    47 "union"                  { count(); return(UNION); }
    48 "unsigned"				{ count(); return(UNSIGNED); }
    48 "unsigned"               { count(); return(UNSIGNED); }
    49 "void"					{ count(); return(VOID); }
    49 "void"                   { count(); return(VOID); }
    50 "volatile"				{ count(); return(VOLATILE); }
    50 "volatile"               { count(); return(VOLATILE); }
    51 "while"					{ count(); return(WHILE); }
    51 "while"                  { count(); return(WHILE); }
    52 
    52 
    53 {L}({L}|{D})*			{ count(); return(check_type()); }
    53 {L}({L}|{D})*            { count(); return(check_type()); }
    54 0[xX]{H}+{IS}?			{ count(); return(CONSTANT); }
    54 0[xX]{H}+{IS}?           { count(); return(CONSTANT); }
    55 0{D}+{IS}?				{ count(); return(CONSTANT); }
    55 0{D}+{IS}?               { count(); return(CONSTANT); }
    56 {D}+{IS}?				{ count(); return(CONSTANT); }
    56 {D}+{IS}?                { count(); return(CONSTANT); }
    57 '(\\.|[^\\'])+'			{ count(); return(CONSTANT); }
    57 '(\\.|[^\\'])+'          { count(); return(CONSTANT); }
    58 
    58 
    59 {D}+{E}{FS}?			{ count(); return(CONSTANT); }
    59 {D}+{E}{FS}?             { count(); return(CONSTANT); }
    60 {D}*"."{D}+({E})?{FS}?	{ count(); return(CONSTANT); }
    60 {D}*"."{D}+({E})?{FS}?   { count(); return(CONSTANT); }
    61 {D}+"."{D}*({E})?{FS}?	{ count(); return(CONSTANT); }
    61 {D}+"."{D}*({E})?{FS}?   { count(); return(CONSTANT); }
    62 
    62 
    63 \"(\\.|[^\\"])*\"		{ count(); return(STRING_LITERAL); }
    63 \"(\\.|[^\\"])*\"        { count(); return(STRING_LITERAL); }
    64 
    64 
    65 ">>="					{ count(); return(RIGHT_ASSIGN); }
    65 ">>="                    { count(); return(RIGHT_ASSIGN); }
    66 "<<="					{ count(); return(LEFT_ASSIGN); }
    66 "<<="                    { count(); return(LEFT_ASSIGN); }
    67 "+="					{ count(); return(ADD_ASSIGN); }
    67 "+="                     { count(); return(ADD_ASSIGN); }
    68 "-="					{ count(); return(SUB_ASSIGN); }
    68 "-="                     { count(); return(SUB_ASSIGN); }
    69 "*="					{ count(); return(MUL_ASSIGN); }
    69 "*="                     { count(); return(MUL_ASSIGN); }
    70 "/="					{ count(); return(DIV_ASSIGN); }
    70 "/="                     { count(); return(DIV_ASSIGN); }
    71 "%="					{ count(); return(MOD_ASSIGN); }
    71 "%="                     { count(); return(MOD_ASSIGN); }
    72 "&="					{ count(); return(AND_ASSIGN); }
    72 "&="                     { count(); return(AND_ASSIGN); }
    73 "^="					{ count(); return(XOR_ASSIGN); }
    73 "^="                     { count(); return(XOR_ASSIGN); }
    74 "|="					{ count(); return(OR_ASSIGN); }
    74 "|="                     { count(); return(OR_ASSIGN); }
    75 ">>"					{ count(); return(RIGHT_OP); }
    75 ">>"                     { count(); return(RIGHT_OP); }
    76 "<<"					{ count(); return(LEFT_OP); }
    76 "<<"                     { count(); return(LEFT_OP); }
    77 "++"					{ count(); return(INC_OP); }
    77 "++"                     { count(); return(INC_OP); }
    78 "--"					{ count(); return(DEC_OP); }
    78 "--"                     { count(); return(DEC_OP); }
    79 "->"					{ count(); return(PTR_OP); }
    79 "->"                     { count(); return(PTR_OP); }
    80 "&&"					{ count(); return(AND_OP); }
    80 "&&"                     { count(); return(AND_OP); }
    81 "||"					{ count(); return(OR_OP); }
    81 "||"                     { count(); return(OR_OP); }
    82 "<="					{ count(); return(LE_OP); }
    82 "<="                     { count(); return(LE_OP); }
    83 ">="					{ count(); return(GE_OP); }
    83 ">="                     { count(); return(GE_OP); }
    84 "=="					{ count(); return(EQ_OP); }
    84 "=="                     { count(); return(EQ_OP); }
    85 "!="					{ count(); return(NE_OP); }
    85 "!="                     { count(); return(NE_OP); }
    86 ";"						{ count(); return(';'); }
    86 ";"                      { count(); return(';'); }
    87 "{"						{ count(); return('{'); }
    87 "{"                      { count(); return('{'); }
    88 "}"						{ count(); return('}'); }
    88 "}"                      { count(); return('}'); }
    89 ","						{ count(); return(','); }
    89 ","                      { count(); return(','); }
    90 ":"						{ count(); return(':'); }
    90 ":"                      { count(); return(':'); }
    91 "="						{ count(); return('='); }
    91 "="                      { count(); return('='); }
    92 "("						{ count(); return('('); }
    92 "("                      { count(); return('('); }
    93 ")"						{ count(); return(')'); }
    93 ")"                      { count(); return(')'); }
    94 "["						{ count(); return('['); }
    94 "["                      { count(); return('['); }
    95 "]"						{ count(); return(']'); }
    95 "]"                      { count(); return(']'); }
    96 "."						{ count(); return('.'); }
    96 "."                      { count(); return('.'); }
    97 "&"						{ count(); return('&'); }
    97 "&"                      { count(); return('&'); }
    98 "!"						{ count(); return('!'); }
    98 "!"                      { count(); return('!'); }
    99 "~"						{ count(); return('~'); }
    99 "~"                      { count(); return('~'); }
   100 "-"						{ count(); return('-'); }
   100 "-"                      { count(); return('-'); }
   101 "+"						{ count(); return('+'); }
   101 "+"                      { count(); return('+'); }
   102 "*"						{ count(); return('*'); }
   102 "*"                      { count(); return('*'); }
   103 "/"						{ count(); return('/'); }
   103 "/"                      { count(); return('/'); }
   104 "%"						{ count(); return('%'); }
   104 "%"                      { count(); return('%'); }
   105 "<"						{ count(); return('<'); }
   105 "<"                      { count(); return('<'); }
   106 ">"						{ count(); return('>'); }
   106 ">"                      { count(); return('>'); }
   107 "^"						{ count(); return('^'); }
   107 "^"                      { count(); return('^'); }
   108 "|"						{ count(); return('|'); }
   108 "|"                      { count(); return('|'); }
   109 "?"						{ count(); return('?'); }
   109 "?"                      { count(); return('?'); }
   110 
   110 [ \t\v\n\f]              { count(); }
   111 [ \t\v\n\f]				{ count(); }
   111 .                        { printf ("Unknown Token: %s\n", yytext); }
   112 .						{ printf ("Unknown Token: %s\n", yytext); }
       
   113 %%
   112 %%
   114 
   113 
   115 int yywrap()
   114 int yywrap()
   116 {
   115 {
   117 	/* stop after eof */
   116     /* stop after eof */
   118 	return 1;
   117     return 1;
   119 }
   118 }
   120 
   119 
   121 int count()
   120 int count()
   122 {
   121 {
   123 	int i;
   122     int i;
   124 
   123 
   125 	for (i = 0; yytext[i] != '\0'; i++)
   124     for (i = 0; yytext[i] != '\0'; i++)
   126 		if (yytext[i] == '\n')
   125         if (yytext[i] == '\n')
   127 			column = 0;
   126             column = 0;
   128 		else if (yytext[i] == '\t')
   127         else if (yytext[i] == '\t')
   129 			column += 4 - (column % 4);
   128             column += 4 - (column % 4);
   130 		else
   129         else
   131 			column++;
   130             column++;
   132 
   131 
   133 	return column;
   132     return column;
   134 }
   133 }
   135 
   134 
   136 int check_type()
   135 int check_type()
   137 {
   136 {
   138 	return IDENTIFIER;
   137     return IDENTIFIER;
   139 }
   138 }
   140 
   139 
   141 void comment() 
   140 void comment()
   142 {
   141 {
   143 	int c;
   142     int c;
   144 
   143 
   145 	c = input(); 
   144     c = input();
   146 	do { 
   145     do {
   147 		while ( c != '*') 
   146         while ( c != '*')
   148 			c = input();
   147             c = input();
   149 		c=input();
   148         c=input();
   150 	} while(c != '/');
   149     } while(c != '/');
   151 }
   150 }