0% found this document useful (0 votes)
64 views12 pages

Compiler Design LAB: Submitted by

The document summarizes 4 programs developed as part of a Compiler Design lab: 1. A simple calculator program that implements a lexical analyzer, parser, and interpreter for basic arithmetic expressions. 2. A lexical analyzer for the C language that tokenizes C code and prints the tokens. 3. A parser for the C language that validates the syntax of C programs and prints whether they are correct or contain errors. 4. A program that generates assembly-level address code for selected C statements like addition, subtraction, multiplication and division.

Uploaded by

abbya_123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views12 pages

Compiler Design LAB: Submitted by

The document summarizes 4 programs developed as part of a Compiler Design lab: 1. A simple calculator program that implements a lexical analyzer, parser, and interpreter for basic arithmetic expressions. 2. A lexical analyzer for the C language that tokenizes C code and prints the tokens. 3. A parser for the C language that validates the syntax of C programs and prints whether they are correct or contain errors. 4. A program that generates assembly-level address code for selected C statements like addition, subtraction, multiplication and division.

Uploaded by

abbya_123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

COMPILER DESIGN LAB

SUBMITTED BY: ABHISHEK ARORA

203/CO/09

INDEX
S.NO. TITLE

Program to implement a simple calculator

Program for Lexical Analyzer for C language

Program for Parser for C language

Program to generate address code for selective C statements

1. PROGRAM TO IMPLEMENT A SIMPLE CALCULATOR


Calculate.y
%{ #include <stdlib.h> #include <stdio.h> int yylex(void); #include "y.tab.h" %} %token INTEGER %% program: line program | line line: expr '\n' { printf("%d\n",$1); } | 'n' expr: expr '+' mulex { $$ = $1 + $3; } | expr '-' mulex { $$ = $1 - $3; } | mulex { $$ = $1; } mulex: mulex '*' term { $$ = $1 * $3; } | mulex '/' term { $$ = $1 / $3; } | term { $$ = $1; } term: '(' expr ')' { $$ = $2; } | INTEGER { $$ = $1; } %% void yyerror(char *s) { fprintf(stderr,"%s\n",s); return; }

yywrap() { return(1); } int main(void) { /*yydebug=1;*/ yyparse(); return 0; }

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

6++1 syntax errorabhishek@linux-hhyb:~/Desktop>

2. PROGRAM FOR A LEXICAL ANALYZER FOR C LANGUAGE


Lex.l
%{ /* need this for the call to atof() below */ #include <math.h> %} DELIM WHITESPACE DIGIT LETTER ID NUMBER %% {DIGIT}+ { printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); } {DIGIT}+ "."{DIGIT}* { printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); } if|else|return|main|include|int|float|char { printf( "A keyword: %s\n", yytext ); } {ID} printf( "An identifier: %s\n", yytext ); "+"|"-"|";"|"="|"("|")"|"{"|"}"|"<"|">"|"*"|"/" printf( "An operator: %s\n", yytext ); "{"[^}\n]*"}" /* eat up one-line comments */ [ \t\n]+ /* eat up whitespace */ . printf( "Unrecognized character: %s\n", yytext ); %% main( argc, argv ) int argc; char **argv; { [ \t\n] {DELIM}+ [0-9] [a-zA-Z] {LETTER}({LETTER}|{DIGIT})* {DIGIT}+(\.{DIGIT}+)?(e[+\-]?{DIGIT}+)?

++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>

3. PROGRAM FOR A PARSER FOR C LANGUAGE


Lex.l

%{ #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 (); }

int yyerror (char *s) { fprintf (stderr, "%s\n", s); }

SAMPLE INPUT PROGRAMS


1) int main() { int a; int b; int c; double d; float e; a=10; b=20; c=a+b; return 0; } 2) int main() { int a; int b; int c; double d; float e; a=10; b=20; c=a+b*; return 0; }

SAMPLE OUTPUT
1) Correct 2) Parse Error

4. PROGRAM TO GENERATE ADDRESS CODE

FOR SELECTIVE C STATEMENTS


#include<stdio.h> #include<string.h> main() { char str[10]; int i,j; char op; printf(\nEnter the expression:); scanf("%s",str); op=str[0]; i=0; while((op!='0')||(op!='+')||(op!='-')||(op!=*)||(op!=/)) { op=str[++i]; } switch(op) { case '+':printf("MOV R2,%c\n",str[i-1]); printf("MOV R3,%c\n",str[i+1]); printf("ADD R1,R2,R3\n"); printf("STORE R1,%c\n",str[i-3]); break; case '-':printf("MOV R2,%c\n",str[i-1]); printf("MOV R3,%c\n",str[i+1]); printf("SUB R1,R2,R3\n"); printf("STORE R1,%c\n",str[i-3]); break; case '*':printf("MOV R2,%c\n",str[i-1]); printf("MOV R3,%c\n",str[i+1]); printf("MUL R1,R2,R3\n"); printf("STORE R1,%c\n",str[i-3]); break; case '/':printf("MOV R2,%c\n",str[i-1]); printf("MOV R3,%c\n",str[i+1]); printf("DIV R1,R2,R3\n"); printf("STORE R1,%c\n",str[i-3]); break; default:printf("ERROR\n"); break; } }

OUTPUT

Enter the expression: x=a+b MOV R2,a MOV R3,b ADD R1,R2,R3 STORE R1,x

You might also like