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

Calculator

This Lex file defines patterns and associated actions to tokenize input expressions for a calculator program. It recognizes numbers and operators and reports errors for invalid characters. The generated lexer is used along with a Yacc-generated parser to create a program that can evaluate simple arithmetic expressions.

Uploaded by

bentitesfa881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Calculator

This Lex file defines patterns and associated actions to tokenize input expressions for a calculator program. It recognizes numbers and operators and reports errors for invalid characters. The generated lexer is used along with a Yacc-generated parser to create a program that can evaluate simple arithmetic expressions.

Uploaded by

bentitesfa881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Calculator.

l
Header Section
%{
#include "y.tab.h"
%}

This part is called the "header section" and is enclosed within `%{` and `%}`. It allows you to include C code
directly in the Lex file. Here, it includes the header file `y.tab.h`, which is generated by Yacc and contains
definitions for tokens.

Rules Section
%%

[0-9]+ { yylval = atoi(yytext); return NUM; }


[-+*/\n] { return *yytext; }
[ \t] ; // ignore whitespace
. { fprintf(stderr, "Invalid character: %s\n", yytext); }

This is the "rules section" where you define patterns and corresponding actions. Each rule consists of a pattern on
the left and an action on the right.

`[0-9]+`: Matches one or more digits. When this pattern is matched, the associated action sets `yylval` to the
integer value of the matched text (`atoi(yytext)`) and returns the token type `NUM`.

`[-+*/\n]`: Matches one of the characters '-', '+', '*', '/', or '\n'. The associated action returns the ASCII value of the
matched character as the token.

`[ \t]`: Matches any space or tab character. The action is empty, indicating that whitespace characters are
ignored.

`.`: Matches any character not matched by the previous patterns. The action prints an error message to stderr,
indicating that an invalid character was encountered.

Conclusion of Rules Section


%%

This marks the end of the rules section.

Additional Functions
int yywrap() {
return 1;
}

This function is called when the end of the input stream is reached. It returns 1, indicating that there is no more
input. In simple cases, this function can be left as is.

In summary, this Lex file defines patterns and associated actions to tokenize input expressions. It recognizes
numbers and operators and reports errors for invalid characters. The generated lexer (`lex.yy.c`) is used along
with the Yacc-generated parser (`y.tab.c`) to create a calculator program that can evaluate simple arithmetic
expressions.

Calculator.y
Header Section

%{
#include <stdio.h>
#include <stdlib.h>
%}
The header section includes C code that will be directly copied into the generated parser file (`y.tab.c`). It
includes necessary standard C library headers.

Yacc Declarations
%token NUM
%left '+' '-'
%left '*' '/'

These lines declare tokens and specify the precedence and associativity of operators. `NUM` is a token
representing numeric literals. The `%left` declarations specify the associativity of operators (`+` and `-` are left-
associative, also `*` and `/` are also left-associative).

Grammar Rules

%%

calc: /* empty */
| calc expr '\n' { printf("Result: %d\n", $2); }
;

expr: NUM { $$ = $1; }


| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr {
if ($3 == 0) {
fprintf(stderr, "Error: Division by zero\n");
exit(1);
}
$$ = $1 / $3;
}
;

These are the Yacc rules that define the grammar of the calculator language. The rules describe how to recognize
and construct abstract syntax trees for expressions.

`calc` is the starting rule, representing a sequence of expressions. When an expression is encountered followed
by a newline character, it prints the result of the expression.

`expr` defines the grammar for arithmetic expressions. It handles numeric literals, addition, subtraction,
multiplication, and division. Division by zero is checked, and an error message is printed if encountered.

Conclusion of Rules Section


%%

This marks the end of the rules section.

main Function
int main() {
yyparse();
return 0;
}

The main function is where the parser is invoked. It calls `yyparse()`, which is a function generated by Yacc to
initiate the parsing process.

You might also like