Question
This is lex file : %{ #include void yyerror(char *); int LineNo = 1; %} DIGIT [0-9] ID [a-z][a-z0-9]* %% IF|ELSE|WHILE|DO|SWITCH|BREAK|CASE|continue|goto|identifier|id|return [a-z] { printf( t%dttt%stt
This is lex file :
%{
#include
void yyerror(char *);
int LineNo = 1;
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
IF|ELSE|WHILE|DO|SWITCH|BREAK|CASE|continue|goto|identifier|id|return
[a-z] { printf( "\t%d\t\t\t%s\t\t letter ", LineNo, yytext);}
[A-Z] { printf( "\t%d\t\t\t%s\t\t Letter ", LineNo, yytext);}
{DIGIT}+ { printf( "\t%d\t\t\t%s\t\t Num ", LineNo, yytext);}
{DIGIT}*"."{DIGIT}+ { printf( "\t%d\t\t\t%s\t\t Float ", LineNo, yytext);}
{ID} { printf( "\t%d\t\t\t%s\t\t Identifier ", LineNo, yytext);}
";" { printf( "\t%d\t\t\t%s\t\t Semicolon ", LineNo, yytext);}
":" { printf( "\t%d\t\t\t%s\t\t Colon ", LineNo, yytext);}
"," { printf( "\t%d\t\t\t%s\t\t Comma ", LineNo, yytext);}
"+"|"-"|"*"|"/"|"++"|"--"|"%" { printf( "\t%d\t\t\t%s\t\t Arithmetic_Operator ", LineNo, yytext);}
":=" { printf( "\t%d\t\t\t%s\t\t Assign_Operator ", LineNo, yytext);}
"<"|"<="|"=="|"!="|">="|">" { printf( "\t%d\t\t\t%s\t\t Comparison_Operator ", LineNo, yytext);}
"&&" { printf( "\t%d\t\t\t%s\t\t Logical_Operator ", LineNo, yytext);}
"("|")" { printf( "\t%d\t\t\t%s\t\t Parentheses ", LineNo, yytext);}
"{"|"}" { printf( "\t%d\t\t\t%s\t\t Braces ", LineNo, yytext);}
{ID}\[{DIGIT}\] { printf( "\t%d\t\t\t%s\t\t Array ", LineNo, yytext);}
"{"[^} ]*"}" /* eat up one-line comments */
[ \t] ; /* skip whitespace */
LineNo++;
. printf("\t%d\t\t\tERROR\t\t Unknown character ", LineNo);
%%
int main(int argc, char **argv) {
++argv,--argc;
if(argc > 0)
yyin = fopen(argv[0],"r");
else
yyin = stdin;
printf(" Line Number\t\t The Lexeme\t\t Type ");
printf("----------------------------------------------------------------------------- ");
FILE * yyout;
yyout = fopen("token.txt","w");
return yylex();
}
void yyerror(char *err) {
printf("yyerror: %s ", err);
}
int yywrap(void){
return 1;
}
This is yac file:
%{
#include
void yyerror(char *);
int yylex(void);
int sym[26];
%}
%token INTEGER VARIABLE
%token WHILE IF ELSE DO SWITCH BREAK CASE continue goto identifier id return
%left '+' '-'
%left '*' '/'
%nonassoc IFX
%nonassoc ELSE
%%
program: program statement ' '
| /* NULL */
;
statement_list: statement statement_list
;
statement: expression { printf("%d ", $1); }
| VARIABLE '=' expression { sym[$1] = $3; }
| WHILE '('expression')' statement { $$ = opr(WHILE,2,$3,$5);}
| IF '('expression')'statement %prec IFX { $$ = opr(IF,2,$3,$5);}
| IF '('expression')' statement ELSE statement {$$ = opr(IF,3,$3,$5,$7);}
| DO statement WHILE'(' expression ')';
| SWITCH '(' expression ')' statement;
| CASE'('expression')' ':' statement;
| BREAK; |continue;
| return '('expression')';| goto id;
;
expression: INTEGER
| VARIABLE { $$ = sym[$1]; }
| expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { $$ = $1 / $3; }
;
term: literal
| identifier
|'('expression')';
literal: literal
|identifier
|'('expression')'
;
%%
void yyerror(char *s) { fprintf(stderr, "%s ", s);}
int main(void) { yyparse();}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
the input for the lexical analyzer is a text file SOURCE.TXT consisting of several lines of text (a program) containing sequences of lexemes corresponding to the above definitions, whitespaces and comments. The input program can be incorrect so your lexical analyzer should generate error messages indicating the line(s) of these errors.
The output of your lexical analyzer consists of 2 text files ST.TXT and TOKENS.TXT. 1. ST.TXT is the symbol table created by the lexical analyzer. Each line consists of three parts: line number the lexeme (string) type (string) ,being one of the following : keyword, identifier, num
i will do st.txt file but my problem with token.txt. How can i do this ?
how can i do token.txt ?
TOKENS.TXT is the list of tokens produced by the lexical analyzer with the following structure: one line of input (in the order of appearance in SOURCE.TXT) corresponding pairs token, attribute, each in separate line in the order as they occur in the line blank line The attribute of keyword, identifier or a number is the line number in the symbol table. The attribute of any other token is lexeme itself. The longest prefix of the input that can match any regular expression pi is taken as the next token
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started