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

Compiler Edesign Exp 3a

The document contains a LEX and YACC file for parsing arithmetic expressions in a programming context. It defines tokens for identifiers, numbers, and operators, along with grammar rules for valid expressions. The main function prompts the user for an expression and checks its validity, providing feedback accordingly.

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 3a

The document contains a LEX and YACC file for parsing arithmetic expressions in a programming context. It defines tokens for identifiers, numbers, and operators, along with grammar rules for valid expressions. The main function prompts the user for an expression and checks its validity, providing feedback accordingly.

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(art.l)
%{
#include "y.tab.h"
%}

%%
[a-zA-Z_][a-zA-Z_0-9]* return id;
[0-9]+(\.[0-9]*)? return num;
[+/*] return op;
. return yytext[0];
\n return 0;

%%
int yywrap() {
return 1;
}

YACC FILE(arth.y)

%{
#include <stdio.h>
int valid = 1;
%}

%token num id op // Define tokens here

%%

// Grammar rules for parsing expressions


start : id '=' s ';'
;

s : id x
| num x
| '-' num x
| '(' s ')' x
;

x : op s
| '-' s
|
;

%%

// Error handling function


int yyerror() {
valid = 0;
printf("\nInvalid expression!\n");

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

Department: Computer science & engineering Reg No: 311522104002

return 0;
}

// Main function
int main() {
printf("\nEnter the expression:\n");
yyparse();

if (valid) {
printf("\nValid expression!\n");
}

return 0;
}

OUTPUT:

Page no:

You might also like