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