Compiler Design Final
Compiler Design Final
THEORY:
LEX program to count the number of words:
Code:
%{
#include<stdio.h> #include<string.h>
int i = 0;
%}
/* Rules Section*/
%%
%%
main()
OUTPUT:
LEX program to count the number of vowels and consonants in a string.
Code: %{ int
vow_count=0; int
const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
yylex();
return 0;
}
EXPERIMENT-1(B)
THEORY:
YACC program to implement a Calculator and recognize a valid Arithmetic
expression.
Code:
%{
/* Definition section */
#include<stdio.h>
#include "y.tab.h"
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
return 1;
}
Parser Source Code:
%{
/* Definition section */
#include<stdio.h> int
flag=0;
%}
%token NUMBER
/* Rule Section */
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression :\n"); yyparse(); if(flag==0)
void yyerror()
flag=1;
OUTPUT:
EXPERIMENT-2
THEORY:
CODE:
import ply.lex as lex
import ply.yacc as yacc
# Define tokens
tokens = (
'A',
'B',
)
# Ignored characters
t_ignore = ' \t\n'
def p_non_equal_ab(p):
'''
non_equal_ab : non_equal_ab A
| non_equal_ab B
| BA
'''
pass
def p_AB(p):
'''
AB : A B
'''
pass
def p_BA(p):
'''
BA : B A
'''
pass
def p_empty(p):
'empty :'
pass
# Error handling rule for syntax errors
def p_error(p):
raise Exception("Syntax error in input!")
result = check_string(input_string)
print(result)
OUTPUT:
EXPERIMENT-3
THEORY:
CODE:
import ply.lex as lex
import ply.yacc as yacc
def p_statement(p):
'''
statement : ID
| IF
| ELSE
| WHILE
| FOR
| BREAK
| CONTINUE
| RETURN
'''
print(f"'{p[1]}' is a keyword")
def p_empty(p):
'empty :'
pass
check_keywords(input_string)
OUTPUT:
EXPERIMENT-4
THEORY:
CODE:
import ply.lex as lex
import ply.yacc as yacc
# Lexer tokens
tokens = (
'ID', # Non-terminals
'ARROW', # Arrow (->)
'OR', #|
)
# Error handling
def t_error(t):
print(f"Illegal character '{t.value[0]}'")
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
def p_production(p):
"""production : ID ARROW rules"""
non_terminal = p[1]
grammar[non_terminal] = p[3]
def p_rules(p):
"""rules : rules OR rule
| rule"""
if len(p) == 4:
p[0] = p[1] + [p[3]]
else:
p[0] = [p[1]]
def p_rule(p):
"""rule : ID"""
p[0] = p[1]
def p_epsilon(p):
"""rule : epsilon"""
p[0] = []
def p_error(p):
if p:
print(f"Syntax error at '{p.value}'")
else:
print("Syntax error at EOF")
if alpha_productions:
new_grammar[non_terminal] = [beta + new_non_terminal for beta in beta_productions]
new_grammar[new_non_terminal] = [alpha for alpha in alpha_productions] + ['ε']
else:
new_grammar[non_terminal] = productions
return new_grammar
if __name__ == "__main__":
input_data = """
A -> A a | B
B -> b
"""
lexer.input(input_data)
print("Original Grammar:")
print_grammar(grammar)
new_grammar = remove_left_recursion(grammar)
OUTPUT: