0% found this document useful (0 votes)
51 views2 pages

Number (0-9) + %% (/ Skip Blanks /) (Number) (Sscanf (Yytext,"%lf",&yylval) Return INTEGER ) - (Return Yytext (0) ) /N (Return Yytext (0) )

The document outlines a program to build a simple pocket calculator using yacc and lex that can parse expressions like 1+(10-5-3)*5+2 and print the result, with details provided on the input to yacc and lex. It describes the overall structure, with a lexical analyzer section defining tokens for numbers and operators, and a yacc parser section defining expression rules for operators like addition and multiplication with precedence. Compilation instructions are given to generate the y.tab.c file and link it with the lex output and C libraries.
Copyright
© © All Rights Reserved
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)
51 views2 pages

Number (0-9) + %% (/ Skip Blanks /) (Number) (Sscanf (Yytext,"%lf",&yylval) Return INTEGER ) - (Return Yytext (0) ) /N (Return Yytext (0) )

The document outlines a program to build a simple pocket calculator using yacc and lex that can parse expressions like 1+(10-5-3)*5+2 and print the result, with details provided on the input to yacc and lex. It describes the overall structure, with a lexical analyzer section defining tokens for numbers and operators, and a yacc parser section defining expression rules for operators like addition and multiplication with precedence. Compilation instructions are given to generate the y.tab.c file and link it with the lex output and C libraries.
Copyright
© © All Rights Reserved
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
You are on page 1/ 2

Submitted by 04CS3009,Puneet Jain

Assignment No 03:
We want to construct a simple pocket-calculator program using yacc and lex
which can parse strings such as 1+(10-5-3)*5+2 and print the result, 13 in this
case. Outline the overall structure of your program components. Give full
details of the input to yacc and lex. (Precise syntactic details are not important,
but your answer should reflect basic principles involved)

Make following assumption :


- Only three binary operator are allowed i.e. plus, minus and multiply
- Only integer data type is allowed
- Parentheses are used to override precedence.

Compile the program using following commands

$lex calculator.l
$yacc calculator.y
$cc y.tab.c –ly -ll

****************Lexical Analyzer**************************

number [0-9]+
%%
[ ] { /*skip blanks*/}

{number} {
sscanf(yytext,"%lf",&yylval);
return INTEGER;
}

. {return yytext[0];
}
\n {return yytext[0];
}

*****************Yacc Pharser ************************


%{
#include <ctype.h>
#include <stdio.h>
#define YYSTYPE double
%}

%token INTEGER
%left '+' '-'
%left '*'
%right UMINUS

%%
lines : lines expr '\n' {printf("%g\n",$2);}
| lines '\n'
|
| error '\n' {yyerror("reenter last line :");
yyerrok;}
;

expr : | expr '+' expr {$$=$1+$3;}


| expr '-' expr {$$=$1-$3;}
| expr '*' expr {$$=$1*$3;}
| '(' expr ')'{$$=$2;}
| '-' expr %prec UMINUS {$$=-$2;}
| INTEGER
;

%%
#include "lex.yy.c"

You might also like