Compiler Design LAB: Submitted by

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy