0% found this document useful (0 votes)
4 views7 pages

Compiler ZX

Uploaded by

Zx Ankit
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)
4 views7 pages

Compiler ZX

Uploaded by

Zx Ankit
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/ 7

Ankit Kumar

23BCE0032

Question-1

aim:-Parsing and Validating Assignment Statements in a Simple


Programming Language
Create a YACC program that uses Context-Free Grammar (CFG) to validate
assignment statements of the form id = expression;. The program should ensure that
the assignment follows the correct syntax, including the proper use of identifiers,
operators, and the required semicolon at the end

Code-
Output:
Question 2:
Parsing and Validating Control Flow Statements in a Simple Programming Language
2. You are working on the development of a custom programming language
designed for teaching basic control flow concepts such as conditional statements (if),
loops (while, for), and assignment operations. To ensure that the code written in this
language is syntactically correct, you need to implement a YACC program that
validates control flow statements. The program should check whether if, while, and
for statements conform to the syntax rules defined by the language. Create a YACC
program that uses Context-Free Grammar (CFG) to validate control flow statements,
specifically: • if statements of the form if (Condition) { id = exp; } • while statements of
the form while (Condition) { id = exp; } • for statements of the form for (id = number;
Condition; increment/decrement) { id = exp; }

CODE:-
OUTPUT:-
AIM:
Simple Calculator Program by Semantic rules and using YACC The university wants
to provide a simple command-line calculator for students to quickly check arithmetic
expressions while working on programming assignments using expression grammar.
You are asked to develop a calculator using YACC with semantic rules that: •
Accepts arithmetic expressions with integers. • Supports operators: o Addition (+),
Subtraction (-), o Multiplication (*), Division (/), o Parentheses ( ) for precedence. •
Evaluates the result immediately after pressing Enter. • Displays an error if division
by zero occurs.

Code:
lex—>
%{
#include "y.tab.h"
%}
%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[+\-*/()] { return yytext[0]; }
[ \t] ; // ignore spaces
\n { return '\n'; }
. { return yytext[0]; }
%%

yacc—->
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}

%token NUMBER
%left '+' '-'
%left '*' '/'
%left UMINUS

%%
input : /* empty */
| input line
;

line : expr '\n' { printf("Result = %d\n", $1); }


;

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


| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { if ($3 == 0) {
yyerror("Division by zero");
exit(1);
} else $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
| '-' expr %prec UMINUS { $$ = -$2; }
| NUMBER
;
%%

void yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
}

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

Output:

You might also like