Compiler Design Lab Assignment 2 - 21BDS0214
Compiler Design Lab Assignment 2 - 21BDS0214
AIM:
To check whether the given string is valid or not using YACC and Lex
programming tools.
ALGORITHM:
STEP 1 : Start the lex program that tokenizes input into tokens, import
necessary header files including `stdio.h’ and the Yacc-generated header file
(`y.tab.h’).
STEP 2 : Specify the patterns and corresponding actions using regular
expressions in the Lex file. - The regular expression ‘[a-zA-Z]+’ matches one or
more alphabetical characters, and the corresponding action is to return the
token VARIABLE. - The regular expression ‘[0-9]+’ matches one or more digits,
and the corresponding action is to return the token NUMBER. - The regular
expression ‘[\t]’ matches a tab character. - The regular expression ‘[\n]’
matches a newline character and returns 0 to indicate the end of input. - The (.)
regular expression matches any other character, and the corresponding action
is to return the first character of the input (‘yytext[0]’).
STEP 3: The ‘yywrap()’ function is defined to indicate the end of input.The Lex
specification file serves as the lexer or tokenizer, breaking down the input into
tokens based on the specified patterns. The tokens are then passed to the Yacc-
generated parser for further processing and analysis.
STEP 4: The Yacc file(.y) defines a grammar and associated actions for validating
arithmetic expressions.Import the necessary header files, including ‘stdio.h’.
STEP 5: Define the tokens that will be used in the grammar (‘NUMBER’ and
‘VARIABLE’) using the ‘%token’ directive.Specify the precedence and
associativity of operators using the ‘%left’ directive. In this case + , - , * , / , % , (
, ) have left associativity.
STEP 6: Define the grammar rules using the production rules specified by the S
and E statements. Each rule represents a valid structure for arithmetic
expressions. - The ‘S’ rule represents the starting point of the grammar. It
specifies that a variable followed by an equal sign (=) and an expression (E) is a
valid arithmetic expression. - The ‘E’ rule defines the different possible
structures for an expression. It covers addition, subtraction, multiplication,
division, modulus, parentheses, numbers and variables.
STEP 7: Define the ‘main()’ function, which prompts the user to enter an
arithmetic expression and call ‘yyparse()’ to start the parsing process.Define
the `yyerror()` function, which is called when an error occurs during parsing. In
this case, it simply prints a message indicating that the entered arithmetic
expression is invalid.
SOURCE CODE:
LEX PROGRAM:
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z]+ return VARIABLE;
[0-9]+ return NUMBER;
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC PROGRAM
%{
#include<stdio.h>
%}
%token NUMBER
%token VARIABLE
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
S: VARIABLE'='E {
printf("\nEntered arithmetic expression is Valid\n\n");
return 0;
}
E:E'+'E
|E'-'E
|E'*'E
|E'/'E
|E'%'E
|'('E')'
| NUMBER
| VARIABLE
;
%%
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Addition,
Subtraction,
Multiplication, Divison, Modulus and Round brackets:\n");
yyparse();
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
}
SCREEN OF EXECUTION:
YACC & LEX PROGRAMS
OUTPUT: