CD Week 4
CD Week 4
1.Aim: Check whether string aaabbb is accepted by given grammar below using YACC or
not S-> aSb|S
Description: Parsing is the process of analyzing a string of symbols to determine its syntactic
correctness according to a given grammar. This process is performed by a parser, which
checks whether an input string follows the production rules of a formal grammar.
• The lexer (Lex/Flex) converts the input into tokens (A for 'a', B for 'b').
• The parser (YACC) matches the tokens against the grammar.
• If the string matches the grammar rules, it is accepted; otherwise, it is rejected.
Functions used:
• yyparse(): This function is the main parser generated by YACC, which processes tokens
according to the grammar rules. It repeatedly calls yylex() to get tokens and determines if
the input string is valid or not.
• yyerror(char *msg): This function is called when a syntax error occurs during parsing. It
prints "Rejected" and terminates the program using exit(1).
• yywrap(): This function is called by yylex() when the end of the input file is reached.
Returning 1 indicates no more input is available.
Algorithm:
Code:
Lex program:
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
\n return '\n';
. return 0; // Invalid character
%%
int yywrap() {
return 1;
}
YACC program:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(char *msg);
%}
%token A B
%%
start : S '\n' { printf("Accepted\n"); return 0; }
S :ASB
|;
%%
int main() {
printf("Enter the string:\n");
yyparse();
}
Output1 :
Conclusion:
The YACC-based parser effectively verifies whether a given string follows the specified
grammar. It ensures that every 'a' has a matching 'b' in the correct order using recursive
parsing. The lexer converts input characters into tokens, while the parser applies grammar
rules for validation. If the input follows the expected pattern, it is accepted; otherwise, it is
rejected.
Description:
This program validates arithmetic expressions containing numbers, identifiers, and operators
(+, -, *, /) using Lex (Flex) for lexical analysis and YACC (Bison) for syntax analysis.
Lex (Lexer):
• Recognizes numbers ([0-9]+) and assigns them as NUMBER tokens.
• Recognizes identifiers ([a-zA-Z]+) and assigns them as ID tokens.
• Ignores whitespace and tabs.
• Returns operators and parentheses as single-character tokens.
YACC (Parser):
• Defines grammar rules for arithmetic expressions.
• Supports operators (+,-,*,/) with correct precedence and associativity.
• Handles negative numbers and identifiers.
• Uses yyerror() to detect invalid expressions.
Algorithm:
• Read the input string and tokenize numbers, identifiers, and operators.
• Skip whitespace and invalid characters.
• Start parsing from the expr non-terminal.
• Apply operator precedence and associativity using %left.
• Use recursive rules to recognize valid expressions.
• Single numbers (NUMBER) and identifiers (ID) are valid.
• Operators follow precedence (*, / before +, -).
• Parentheses enforce priority.
• If an unexpected token is found, yyerror() prints "Expression is invalid".
• If parsing completes successfully, "Expression is valid" is printed.
• Otherwise, an error message is displayed.
Code:
Lex program:
%{
#include"y.tab.h"
extern yylval;
%}
/* defined section */
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;} //this is send to the yacc code as token
INTEGER
[a-zA-Z]+ {return ID;} //this is send to the yacc code as token ID
[\t]+ ;
\n {return 0;}
. {return yytext[0];}
%%
YACC program:
%{
#include<stdio.h>
%}
%token NUMBER ID
%left '+' '-'
%left '*' '/'
%%
expr: expr '+' expr
|expr '-' expr
|expr '*' expr
|expr '/' expr
|'-'NUMBER
|'-'ID
|'('expr')'
|NUMBER
|ID
;
%%
//main function
main()
{
printf("Enter the expression\n");
yyparse();
printf("\nExpression is valid\n");
exit(0);
}
Output1:
Conclusion: