[go: up one dir, main page]

0% found this document useful (0 votes)
9 views6 pages

Control Structures

The document contains a parser and lexer for a simple programming language that recognizes control structures such as if statements, while loops, switch cases, and for loops. It includes grammar rules and actions for recognized statements, as well as a lexer specification for tokenizing keywords and symbols. The output demonstrates the successful recognition of control structures through user input.
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)
9 views6 pages

Control Structures

The document contains a parser and lexer for a simple programming language that recognizes control structures such as if statements, while loops, switch cases, and for loops. It includes grammar rules and actions for recognized statements, as well as a lexer specification for tokenizing keywords and symbols. The output demonstrates the successful recognition of control structures through user input.
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/ 6

PROGRAM

(cs.y)
%{
#include<stdio.h>
int yylex();
%}

%token IF ELSE WHILE FOR SWITCH CASE DEFAULT OPEN_BRACE CLOSE_BRACE


SEMICOLON COLON OPEN_BRACKET CLOSE_BRACKET BREAK

%%
program: statement
| program statement
;

statement:if_statement
|while_loop
|switch_case_statement
|for_loop
;

if_statement:IF OPEN_BRACKET expression_opt CLOSE_BRACKET OPEN_BRACE


expression_opt CLOSE_BRACE ELSE OPEN_BRACE expression_opt
CLOSE_BRACE
{
printf("Recognized IF Else statement\n");
}
;
while_loop:WHILE OPEN_BRACKET expression_opt CLOSE_BRACKET
OPEN_BRACE
expression_opt CLOSE_BRACE
{
printf("Recognized WHILE loop\n");
}
;
switch_case_statement:SWITCH OPEN_BRACKET expression_opt
CLOSE_BRACKET
OPEN_BRACE case_list CLOSE_BRACE
{
printf("Recognized SWITCH_CASE statement with DEFAULT\n");
}
;
for_loop:FOR OPEN_BRACKET expression_opt SEMICOLON expression_opt
CLOSE_BRACKET OPEN_BRACE expression_opt CLOSE_BRACE
{
printf("Recognized FOR loop\n");
}
;
case_list:CASE expression COLON expression BREAK
SEMICOLON DEFAULT COLON expression_opt
;

expression_opt:/*empty*/
|expression
|expression SEMICOLON
;
expression:
;
%%

int yyerror(const char *s){


fprintf(stderr,"Error=%s\n", s);
return 1;
}

int main() {
printf("\nEnter control structure:");
yyparse();

return 0;
}

(cs.l)
%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
%}

%%
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"for" { return FOR; }
"switch" { return SWITCH; }
"case" { return CASE; }
"default" { return DEFAULT; }
"break" { return BREAK; }
"(" { return OPEN_BRACKET; }
")" { return CLOSE_BRACKET; }
"{" { return OPEN_BRACE; }
"}" { return CLOSE_BRACE; }
";" { return SEMICOLON; }
[\t\n] ;
.;
%%

int yywrap() {
return 1;
}

OUTPUT:
a12-36@a1236-ThinkStation-P330:~$ gedit cs.y
a12-36@a1236-ThinkStation-P330:~$ gedit cs.l
a12-36@a1236-ThinkStation-P330:~$ yacc -d cs.y
cs.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
cs.y: warning: 10 reduce/reduce conflicts [-Wconflicts-rr]
cs.y: note: rerun with option '-Wcounterexamples' to generate conflict
counterexamples
a12-36@a1236-ThinkStation-P330:~$ lex cs.l
a12-36@a1236-ThinkStation-P330:~$ gcc lex.yy.c y.tab.c
y.tab.c: In function ‘yyparse’:
y.tab.c:1257:7: warning: implicit declaration of function ‘yyerror’; did you mean
‘yyerrok’? [-Wimplicit-function-declaration]
1257 | yyerror (YY_("syntax error"));
| ^~~~~~~
| yyerrok
a12-36@a1236-ThinkStation-P330:~$ ./a.out

Enter control structure:if(a<b){


a=5
}
else {
b=5
}
Recognized IF Else statement
^C
a12-36@a1236-ThinkStation-P330:~$ ./a.out

Enter control structure:while(i<10){


i=i+1
}
Recognized WHILE loop

You might also like