




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































#define T_IDENTIFIER 300 #define T_INTEGER 301 #define T_REAL 302 #define T_STRING 303 #define T_ASSIGN 304 #define T_ELSE 305 #define T_IF 306 #define T_THEN 307 #define T_EQ 308 #define T_LT 309 #define T_NE 310 #define T_GE 311 #define T_GT 312 %}
letter [a-zA-Z] digit [0-9] ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*' %%
":=" {printf(" %s ", yytext);return(T_ASSIGN);} "else" {printf(" %s ", yytext);return(T_ELSE);}
letter [a-zA-Z] digit [0-9] ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*' %%
{id} {printf(" %s ", yytext);return(T_IDENTIFIER);} {integer} {printf(" %s ", yytext);return(T_INTEGER);} {real} {printf(" %s ", yytext);return(T_REAL);} {string} {printf(" %s ", yytext);return(T_STRING);} {comment} {/* T_COMMENT /} {ws} {/ spaces, tabs, newlines */}
":=" {printf(" %s ", yytext);return(T_ASSIGN);} "else" {printf(" %s ", yytext);return(T_ELSE);} "then" {printf(" %s ", yytext);return(T_THEN);} "<=" {printf(" %s ", yytext);return(T_EQ);} "<" {printf(" %s ", yytext);return(T_LT);} "<>" {printf(" %s ", yytext);return(T_NE);} ">=" {printf(" %s ", yytext);return(T_GE);} ">" {printf(" %s ", yytext);return(T_GT);}
%%
":=" {return(T_ASSIGN);} "else" {return(T_ELSE);} "then" {return(T_THEN);} "<=" {yylval = T_EQ; return(T_EQ);} ... Etc.... ">" {yylval = T_GT; return(T_GT);}
{id} {yylval = install_id(); return(T_IDENTIFIER);} {integer} {yylval = install_int(); return(T_INTEGER);} {real} {yylval = install_real(); return(T_REAL);} {comment} {/* T_COMMENT /} {ws} {/ spaces, tabs, newlines */}
%% install_id() { /* A procedure to install the lexeme whose first character is pointed to by yytext and whose length is yylen into symbol table and return a pointer */ }
install_int() { /* Similar – but installs an integer lexeme into symbol table */ }
install_real() { /* Similar – but installs a real lexeme into symbol table */ } Docsity.com
yylineno++, yytchar):yytchar)==EOF?0:yytchar)
FILE *yyin={stdin}, *yyout = {stdout};
yyinput () { return(input()); }
yyoutput(c) int c { output(c); }
yyunput(c) int c { upput(c); }
Compilation at Unix Command Line: lex lexfile.l (creates lex.yy.c) cc lex.yy.c –ll (include lex library)
#define T_IDENTIFIER 300 #define T INTEGER 301 #define T_REAL 302 #define T STRING 303 #define T_ASSIGN 304 #define T ELSE 305 #define T_IF 306 #define T_THEN 307 #define T_EQ 308 #define T LT 309 #define T_NE 310 #define T GE 311 #define T_GT 312 #define YYNEWLINE 10 yylex ( ) { int nstr; extern int yyprevious; while((nstr - yylook()) >- 0) yyfussy: switch(nstr) { case 0: if(yywrap()) return(0); break; case 1: {printf(" %s ", yytext);return(TASSIGN);} break; case 2: {printf(" %s ", yytext);return(T_ELSE);} break; case 3: (printf(" %s ", yytext) ;return (T IF) ; } break;
case 4: { #ifdef PRNTFLG printf(" %s ", yytext); #endif return(T_THEN); } break; case 5: {printf(" %s ", yytext);return(T_EQ);} break; case 6: {printf(" %s ", yytext);return(T_LT);} break; case 7: {printf(" %s ", yytext);return(T_NE);) break; case 8: {printf(" %s ", yytext);return(T_GE);} break; case 9: {printf(" %s ", yytext);return(T_GT);} break; case 10: {printf(" %s ", yytext);return(T_IDENTIFIER);} break; case 11: {printf(" %s ", yytext);return(T_INTEGER);) break; case 12: {printf(" %s ", yytext) ;return(T_REAL); } break; case 13: {printf(" %s ", yytext);return(T_STRING);}
#include "y.tab.h" %}
letter [a-zA-Z] digit [0-9]
ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*'
%%
":=" {return(T_ASSIGN);} ":" {return(T_COLON);} "array" {return(T_ARRAY);} "begin" {return(T_BEGIN);} "case" {return(T_CASE);} "const" {return(T_CONST);} "downto" {return(T_DOWNTO);} "do" {return(T_DO);} "else" {return(T_ELSE);} "end" {return(T_END);} "file" {return(T_FILE);} "for" {return(T_FOR);}
"function" {return(T_FUNCTION);} /* "goto" {return(T_GOTO);} / "if" {return(T_IF);} "label" {return(T_LABEL);} "nil" {return(T_NIL);} "not" {return(T_NOT);} "of" {return(T_OF);} / "packed" {return(T_PACKED);} / "procedure" {return(T_PROCEDURE);} "end" {return(T_END);} "program" {return(T_PROGRAM);} "record" {return(T_RECORD);} "repeat" {return(T_REPEAT);} "set" {return(T_SET);} "then" {return(T_THEN);} "to" {return(T_TO);} "type" {return(T_TYPE);} "until" {return(T_UNTIL);} "var" {return(T_VAR);} "while" {return(T_WHILE);} / "with" {return(T_WITH);} */ "+" {return(T_PLUS);} "-" {return(T_MINUS);} "or" {return(T_OR);} "and" {return(T_AND);} "div" {return(T_DIV);} "mod" {return(T_MOD);} "/" {return(T_RDIV);}