0% found this document useful (0 votes)
5 views3 pages

SPCC Exp3

This document outlines Experiment No. 3 for a Computer Engineering course at Universal College of Engineering, focusing on Lex and Yaac programs for parsing expressions. The Lex program defines tokens for numbers and identifiers, while the Yaac program implements grammar rules for arithmetic operations and error handling. The main function prompts the user for an expression and processes it using the defined grammar.

Uploaded by

krutikapandya23
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)
5 views3 pages

SPCC Exp3

This document outlines Experiment No. 3 for a Computer Engineering course at Universal College of Engineering, focusing on Lex and Yaac programs for parsing expressions. The Lex program defines tokens for numbers and identifiers, while the Yaac program implements grammar rules for arithmetic operations and error handling. The main function prompts the user for an expression and processes it using the defined grammar.

Uploaded by

krutikapandya23
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/ 3

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject: SPCC

Experiment No: 3

Roll No:99 Name: Roshni Kamlesh Kumar Yadav Div: B Batch: B3

Lex Program:

%{

/* Definition section*/

#include "y.tab.h"

extern yylval;

%}

%%a

[0-9]+ {

yylval = atoi(yytext);

return NUMBER;

}
[a-zA-Z]+ { return ID; }

[ \t]+ ; /*For skipping whitespaces*/

\n { return 0; }

. { return yytext[0]; }

%%

Yaac Program :

%{
#include <stdio.h>
Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: SPCC

#include <stdlib.h>
%}

%token NUMBER ID

/* Set the precedence and associativity of operators */


%left '+' '-'
%left '*' '/'

%%

/* Grammar rules */
E:T{
printf("Result = %d\n", $$); return 0;
}

T : T '+' T { $$ = $1 + $3; } | T '-' T { $$


= $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| '-' NUMBER { $$ = -$2; }
| '-' ID { $$ = -$2; }
| '(' T ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| ID { $$ = $1; };

%%

int main() {
printf("Enter the expression:\n"); yyparse();
return 0;
}
/* Error handling function */ int
yyerror(const char* s) {
printf("\nExpression is invalid\n"); return 0;
Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: SPCC

/* Required function for lexer */ int yywrap()


{ return 1;
}

Output:

You might also like