Compiler Design LAB: Submitted by
Compiler Design LAB: Submitted by
Compiler Design LAB: Submitted by
203/CO/09
INDEX
S.NO. TITLE
Lexcalc.l
%{ #include <stdlib.h> #include <stdio.h> #include "y.tab.h" void yyerror(char*); extern int yylval; %} %% [ \t]+ ; [0-9]+ {yylval = atoi(yytext); return INTEGER;} [-+*/] {return *yytext;} "(" {return *yytext;} ")" {return *yytext;} \n {return *yytext;} . {char msg[25]; sprintf(msg,"%s <%s>","invalid character",yytext); yyerror(msg);}
OUTPUT
abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> 4+3 7 69-7 62 lex Lexcalc.l yacc d Calculate.y gcc y.tab.c lex.yy.x ./a.out
++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; yylex(); }
File.c
main() { int a1, a2; float f1=9.09; a1=10; if(a1>10) a2=a1; }
OUTPUT
abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> abhishek@linux-hhyb:~/Desktop> A keyword: main An operator: ( An operator: ) An operator: { A keyword: int An identifier: a1 Unrecognized character: , An identifier: a2 An operator: ; A keyword: float An identifier: f1 An operator: = A float: 9.09 (9.09) An operator: ; An identifier: a1 An operator: = lex lex.l gcc c lex.yy.c gcc o final lex.yy.o -lfl ./final file.c
An integer: 10 (10) An operator: ; A keyword: if An operator: ( An identifier: a1 An operator: > An integer: 10 (10) An operator: ) An identifier: a2 An operator: = An identifier: a1 An operator: ; An operator: } ajay@linux-hhyb:~/Desktop>
%{ #include "parserc_tab.h" %} D A Num ID type %% "goto" {return GOTO;} "continue" {return CONTINUE;} "break" {return BREAK;} "return" {return RETURN;} {type} {return TYPE;} {ID} {return IDENTIFIER;} {D}+ {return INTEGERLITERAL;} {D}+"."{D}+ {return FLOATLITERAL;} \"(\\.|[^\\"])*\" {return STRINGLITERAL;} ";" {return ';';} "\n" {return '\n';} "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} "=" {return '=';} "(" {return '(';} ")" {return ')';} "{" {return '{';} "}" {return '}';} %% int yywrap (void) {return 1;} [0-9] [a-zA-Z_] [0-9]* [a-zA-Z_][a-zA-Z_0-9]* "int"|"float"|"void"|"char"|"double"
yacc.y
%{ #include "lex.yy.c" %} %token TYPE %token IDENTIFIER INTEGERLITERAL FLOATLITERAL STRINGLITERAL %token GOTO CONTINUE BREAK RETURN %left '+' '-' %left '*' '/' %right UMINUS %% function_definition : TYPE IDENTIFIER '(' ')' '\n' '{' statement_list '}' '\n' {printf ("Correct\n");} ; statement_list : statement_list statement '\n' | statement_list '\n' | ; statement : jump_statement | assignment_statement | declaration_statement ; jump_statement : GOTO IDENTIFIER ';' | CONTINUE ';' | BREAK ';' | RETURN ';' | RETURN expr ';' ; assignment_statement : IDENTIFIER '=' expr ';' ; declaration_statement : TYPE IDENTIFIER ';' ; expr : expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '(' expr ')' | '-' expr %prec UMINUS | INTEGERLITERAL | FLOATLITERAL | IDENTIFIER ; %% int main() { return yyparse (); }
SAMPLE OUTPUT
1) Correct 2) Parse Error
OUTPUT
Enter the expression: x=a+b MOV R2,a MOV R3,b ADD R1,R2,R3 STORE R1,x