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

Compiler Edesign Exp 3d

The document contains code for a lexical analyzer (LEX) and a parser (YACC) for a simple arithmetic expression evaluator. It defines tokens for numbers and operators, and implements grammar rules for parsing expressions and calculating results. The main function prompts the user to enter expressions and outputs the evaluated result.

Uploaded by

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

Compiler Edesign Exp 3d

The document contains code for a lexical analyzer (LEX) and a parser (YACC) for a simple arithmetic expression evaluator. It defines tokens for numbers and operators, and implements grammar rules for parsing expressions and calculating results. The main function prompts the user to enter expressions and outputs the evaluated result.

Uploaded by

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

MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer science & engineering Reg No: 311522104002

PROGRAM:

LEX FILE(arith1.l):

%{
#include "y.tab.h"
extern int yylval;
int yyerror(const char *);
%}

%%
[ \t] {;} // Ignore whitespace
[0-9]+ { sscanf(yytext, "%d", &yylval); return NUMBER; }
[-+*/\n()] { return *yytext; } // Return each character as token
. { yyerror("unrecognized character"); return(0); }

%%

int yywrap(void) {
return 1;
}

YACC FILE(arithy.y):

%{
#include <stdio.h>

#define YYSTYPE int

int yylex();
int yyerror(const char *s);
%}

%token NUMBER

%left '+' '-'


%left '*' '/'

%%

list:
| list '\n'
| list expr '\n' { printf("Ans:\t%d\n", $2); }
;

expr:
NUMBER { $$ = $1; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }

Page no:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer science & engineering Reg No: 311522104002

| '(' expr ')' { $$ = $2; }


;

%%

int main(void) {
printf("Enter expressions:\n");
yyparse();
return 0;
}

int yyerror(const char *s) {


fprintf(stderr, "*%s*\n", s);
return 0;
}

OUTPUT:

Page no:

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