0% found this document useful (0 votes)
11 views12 pages

A4 67 Prac2

The document contains YACC specifications for various programming tasks, including syntax checking for arithmetic expressions, recognition of specific string patterns, validation of 'do while' loops, and handling nested blocks with braces. Each section includes lexer and parser definitions, along with error handling and main functions for user interaction. The outputs demonstrate the validity of the entered expressions or structures.

Uploaded by

Yash Saver
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)
11 views12 pages

A4 67 Prac2

The document contains YACC specifications for various programming tasks, including syntax checking for arithmetic expressions, recognition of specific string patterns, validation of 'do while' loops, and handling nested blocks with braces. Each section includes lexer and parser definitions, along with error handling and main functions for user interaction. The outputs demonstrate the validity of the entered expressions or structures.

Uploaded by

Yash Saver
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/ 12

Name: Shrinivas patil

Section: A
Roll No: A4-67

Practical - 2

E1 : Write YACC specification to check syntax of a simple expression involving operators +,


-, *

and /. Also convert the arithmetic expression to postfix.

q1.l:

%{

#include "q1.tab.h"

%}

%%

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

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

[\n] { return NL; }

[ \t] ; // ignore whitespace

. { return yytext[0]; }

%%

q1.y :

%{

#include <stdio.h>

#include <stdlib.h>
extern int yylex();

extern char *yytext;

void yyerror(const char *s);

%}

%token NUMBER ID NL

%left '+' '-'

%left '*' '/'

%%

stmt:

exp NL {

printf("\nThe Entered expression is valid.\n");

printf("\n");

exit(0);

| exp1 NL {

printf("\nThe Entered expression is valid.\n");

printf("But calculation cannot be performed on variables.\n");

printf("\n");

exit(0);

exp:

exp '+' exp {printf(" ");$$ = $1 + $3;printf("+"); }

| exp '-' exp {printf(" ");$$ = $1 - $3;printf("-"); }

| exp '*' exp {printf(" ");$$ = $1 * $3;printf("*"); }


| exp '/' exp {printf(" ");$$ = $1 / $3;printf("/"); }

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

| NUMBER {$$ = $1;printf("%d", $1);}

exp1:

exp1 '+' exp1 {printf(" ");printf("+");}

| exp1 '-' exp1 {printf(" ");printf("-");}

| exp1 '*' exp1 {printf(" ");printf("*");}

| exp1 '/' exp1 {printf(" ");printf("/");}

| '(' exp1 ')' {}

| ID {printf("%s", yytext);}

%%

void yyerror(const char *s) {

printf("\nThe Entered expression is invalid.\n");

exit(0);

int main() {

printf("Enter any expression:\n");

yyparse();

return 0;

int yywrap() {

return 1;

}
Output:-

E2: Write YACC specification to recognize strings that can be accepted by following
language:

Batch 4: L={ 0 n 1 n | n&gt;=0} U { 1 n 0 n | n&gt;=0}

q2.l:

%{

#include "q2.tab.h"

%}

%%

0 { return ZERO; }

1 { return ONE; }

\n { return '\n'; }

. { return *yytext; }
%%

int yywrap() { return 1; }

q2.y :

%{

#include <stdio.h>

#include <stdlib.h>

int yylex();

void yyerror(const char *s);

%}

%token ZERO ONE

%%

start : pattern '\n' { printf("Valid string.\n"); return 0; }

pattern : seq01

| seq10

seq01 : zeros ones

seq10 : ones zeros

zeros : /* empty */

| ZERO zeros
;

ones : /* empty */

| ONE ones

%%

void yyerror(const char *s) {

printf("Invalid string.\n");

int main() {

printf("Enter string: ");

yyparse();

return 0;

Output:
E3: To validate syntax of following programing language construct:

Batch 4: do while

q3.l:

%{

#include "q3.tab.h"

%}

%%

"do" return DO;

"while" return WHILE;

"{" return LBRACE;

"}" return RBRACE;

"(" return LPAREN;

")" return RPAREN;

";" return SEMICOLON;

[a-zA-Z][a-zA-Z0-9]* return IDENTIFIER;

[0-9]+ return NUMBER;

[ \t\n] ; /* ignore whitespace */

. return yytext[0]; /* for other operators */

%%

int yywrap() {

return 1;

q3.y:

%{

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

extern int yylex();

void yyerror(const char *s);

%}

%token DO WHILE LPAREN RPAREN LBRACE RBRACE SEMICOLON

%token IDENTIFIER NUMBER

%%

program:

dowhile_stmt

| program dowhile_stmt

dowhile_stmt:

DO LBRACE statements RBRACE WHILE LPAREN expression RPAREN SEMICOLON


{ printf("Valid do-while loop\n"); }

statements:

statement

| statements statement

statement:

IDENTIFIER SEMICOLON

expression:
IDENTIFIER

| NUMBER

| expression '+' expression

| expression '-' expression

| expression '*' expression

| expression '/' expression

| expression '<' expression

| expression '>' expression

| expression "==" expression

| expression "!=" expression

%%

void yyerror(const char *s) {

fprintf(stderr, "Syntax error: %s\n", s);

exit(1);

int main() {

printf("Enter a do-while loop:\n");

yyparse();

return 0;

}
Output:

E4: Implementation Batchwise:

Batch 4: Write a YACC program that handles nested blocks using { } and validates proper

nesting.

q4.l:

%{

#include "q4.tab.h"

%}

%%

"{" return LBRACE;

"}" return RBRACE;

[ \t\n]+ ; /* Ignore whitespace */

. {

fprintf(stderr, "Invalid character: %s\n", yytext);

exit(1);

%%
q4.y:

%{

#include <stdio.h>

#include <stdlib.h>

extern int yylex();

void yyerror(const char *s);

%}

%token LBRACE RBRACE

%%

program:

/* An empty program is valid */

| block

block:

LBRACE RBRACE

| LBRACE block RBRACE

| LBRACE RBRACE block

| block block

%%

void yyerror(const char *s) {

fprintf(stderr, "Error: Mismatched braces.\n");

exit(1);

}
int main() {

printf("Enter a sequence of braces (use { and }):\n");

yyparse();

printf("Valid nesting.\n");

return 0;

int yywrap() {

return 1;

Output:

You might also like