YACC - Compiler Design
YACC - Compiler Design
YACC
Harishkumar J
Sathyapriya R
TOPICS
Working of
Definition
YACC
Declarations
%%
Translation rules
%%
Supporting C rules
Declarations Part
• This part of YACC has two sections; both are optional. The first section has ordinary C
declarations, which is delimited by %{ and %}. Any temporary variable used by the
second and third sections will be kept in this part.
Translation Rule Part
• After the first %% pair in the YACC specification part, we place the translation rules.
Every rule has a grammar production and the associated semantic action.
Supporting C–Rules
• It is the last part of the YACC specification and should provide a lexical analyzer
named yylex(). These produced tokens have the token's name and are associated with
its attribute value. Whenever any token like DIGIT is returned, the returned token name
should have been declared in the first part of the YACC specification.
LEX WITH YACC :
output
ALGORITHM:
• Flex (lexical analyzer) recognizes the input tokens based on the regular
expressions defined in calc.l. It recognizes numbers and whitespace
characters.
• Bison (parser generator) defines the grammar rules and actions for
parsing and evaluating arithmetic expressions. It generates a parser
(yyparse()) based on the rules defined in calc.y.
• In Bison file (calc.y), we define the precedence and associativity of
operators using %left and %token directives.
• In the main() function, you prompt the user to enter an arithmetic
expression, then call yyparse() to parse and evaluate it.
FUNCTIONS USED:
yywrap() – Indicate the end of the input
yyparse()
calls the yylex(), whenever the parser needs the next token in input.
Read the token, matches against grammar rules and execute actions.
yylex() – read input, match against patterns (RE) and returns tokens or perform
actions.
yyerror() – Report syntax errors during process.
atoi() – convert a string representing an integer into an integer value.
yytext - holds the corresponding substring when a pattern is matched in the input
PROGRAM:
Lex program : YACC program :
DOWNLOAD :
https://gnuwin32.sourceforge.net/packages.html
HTTP://MARIN.JB.FREE.FR/BISON/
EXECUTION: