Вы находитесь на странице: 1из 5

Ex.No.

4
GENERATION OF YACC SPECIFICATION FOR A FEW SYNTACTIC CATEGORIES

AIM:
To generate YACC specification for the following syntactic categories:
(i) To recognize a valid arithmetic expression that uses operator +, - , * and /.
(ii) To recognize a valid variable which starts with a letter followed by any number of letters
or digits.
(iii) Implementation of Calculator using LEX and YACC.

Ex4.1:
Aim:
To recognize a valid arithmetic expression that uses operator +, -, * and / using Recursive
Descent Parser:

ALGORITHM:
Step 1: Start the program
Step 2: Input an arithmetic expression and call the function Parser ( ).
Step 3: In Lexer ( ) get the input symbol and match with the look ahead pointer and then return
the token accordingly.
Step 4: In E ( ), check whether the look ahead pointer is + or -else return syntax error.
Step 5: In T ( ), check whether the look ahead pointer is * or / else return syntax error.
Step 6: In F ( ), check whether the look ahead pointer is a member of any identifier.
Step 7: In main ( ), check if the current look ahead points to the token in a given CFG, if it does
not match then return syntax error.
INPUT / OUTPUT

INPUT FILE:
(a+b)*(c/d)-g;
1+5;
5-3;
a+2;

OUTPUT:
identifier: a
arithmetic operator +
identifier: b
arithmetic operator *
identifier: c
arithmetic operator /
identifier: d
arithmetic operator -
identifier: g
number: 1
arithmetic operator +
number: 5
number: 5
arithmetic operator -
number: 3
identifier: a
arithmetic operator +
number: 2
parsed successfully

RESULT:
Thus the program to implement the recursive descent parser was implemented and verified.
Ex 4.2:
TO RECOGNIZE A VALID VARIABLE THAT STARTS WITH A LETTER FOLLOWED
BY ANY NUMBER OF
LETTERS OR DIGITS
AIM:
To write a program using C or LEX and YACC to test whether a given identifier is valid or not.

ALGORITHM:
Step1: Start the program.
Step2: Read the given input string
Step3: Check the initial character of the string is numerical or any special character except _
then print it is not a valid identifer
Step4: Otherwise print it as valid identifier if remaining characters of string doesnt contains
any special characters except _.
Step9: Stop the program

INPUT / OUTPUT:

Enter an Identifier : abcd


Valid Identifier

RESULT:
Thus the program to recognize a valid identifier is executed successfully.
Ex 4.3:
IMPLEMENTATION OF CALCULATOR USING LEX AND YACC

AIM:
To write LEX and YACC program for implementation of Calculator using LEX and YACC Tool.

ALGORITHM:
Step1: Start the Program
Step2: In LEX Program identify the number and return digit to Parser.
Step3: In YACC program declare the possible symbol type for the tokens that are
returned by LEX.
Step4: Define Precedence and Associativity.
Step5: Define rules in CFG for non-terminal.
Step6: In main ( ) get the input expression and print the output.
Step7: Stop the program.

INPUT & OUTPUT:

$lex parser.l
$yacc d parser.y
$cc lex.yy.c y.tab.c ll lm
$ ./a.out
2+3
5.0000

RESULT:
Thus the program for implementation of a calculator using LEX and YACC tools was
successfully done.

Вам также может понравиться