Lab Sheet 4
Write a C program to test whether a given identifier is valid or not.
Objective:
To write a C program that tests whether a given string is a valid identifier according to the rules
of the C programming language.
Algo:
Start
Read the input string (identifier).
Check the first character:
If the first character is not:
a letter (A–Z or a–z), or an underscore _ → Then print “Invalid Identifier” and stop.
For every remaining character in the string (from index 1 to end):
Check if the character is: a letter (A–Z, a–z), OR a digit (0–9), OR an underscore _ If any
character does not match these → Print “Invalid Identifier” and stop.
If all characters satisfy the rules → Print “Valid Identifier”.
Stop
Code:
Lab Sheet 5
Write a C program to simulate lexical analyzer for validating operators.
Objective:
To write a C program that simulates a lexical analyser to identify and validate operators from a
given line of code. This helps in understanding how lexical analysers tokenize source code and
categorize operators like arithmetic, relational, logical, and assignment operators.
Algo:
1. Start
2. Read a line of code into the string line.
3. Initialize index i = 0.
4. Repeat while line[i] is not end of string:
5. Check if the characters line[i] and line[i+1] form any of the following:
o Relational operators:
==, !=, >=, <=
o Assignment operators:
+=, -=, *=, /=
o Logical operators:
&&, ||
6. If a two-character operator is found:
o Store it in op
o Call:
isRelational(op)
isLogical(op)
isAssignment(op)
o Print the operator type
o Move index forward: i = i + 2
o Continue to next iteration
7. Store the current character line[i] in op.
8. Check:
o isArithmetic(line[i]) → arithmetic operator
o isRelational(op) → relational operator
o isLogical(op) → logical operator
o isAssignment(op) → assignment operator
9. If matched, print the operator type.
10. Increment index: i = i + 1
11. Repeat until all characters are scanned.
12. Stop
Code:
Lab Sheet 6
Write a C program for implementing the functionalities of predictive parser.
Objective:
To write a C program to simulate a Predictive Parser using an input string and a hardcoded
predictive parsing table based on a simple grammar.
Algo:
Start
Read the input string and append $.
Initialize the stack with: $ E (start symbol)
Set input pointer i = 0.
Repeat while top of stack ≠ '$':
Let X = top of stack, Let a = current input symbol
If X == a → pop stack and move input (i++)
Use predictive parsing table (function getProduction) to find the production for (X, a)
If no production exists → reject the string
Pop X
Push the production’s RHS in reverse order (unless epsilon)
Anything else → Reject the string
After stack reduces to $, check input symbol:
If input symbol is $ → Accept
Else → Reject
Stop
Code:
Lab Sheet 7
Write a C program for constructing of LL (1) parser.
Objective:
To write a C program to construct an LL(1) parser for a given grammar using a parsing table.
The parser will take an input string and parse it using the LL(1) technique, showing parsing steps
and deciding whether the string is accepted or not.
Algo:
Start
Read the input string and append $.
Initialize stack with:
$,E(start symbol)
Set input pointer i = 0.
Repeat until stack becomes empty:
o Let X = top of stack
o Let a = current input symbol
Case 1: Terminal match
o If X == a, pop the stack and move input pointer (i++)
Case 2: Non-terminal
o Use the LL(1) parsing table (table(X, a)) to find the production
o If no production exists → Reject
o Pop X
o If production ≠ epsilon (e), push its symbols in reverse order
Case 3: Otherwise
o Reject the string
If stack is empty and input symbol is $ → Accept
Otherwise → Reject
Stop
Code:
Labsheet-8
Write a C program to construct recursive descent parsing.
Objective:
To write a C program to implement a Recursive Descent Parser for a given grammar,
demonstrating how top-down parsing works using mutually recursive procedures for non-
terminals.
Algo:
Start
Read the input expression into a string.
Initialize a pointer p = 0 to track the current symbol.
Call procedure E() to start parsing (start symbol of grammar).
In E():
Call T(), Call E'(), In E'():
If next character is +, match it, call T(), then call E'() again.
In T(): Call F() Call T'()
In T'():
If next character is *, match it, call F(), then call T'() again.
In F():
If next symbol is i, match it (identifier)
Else if (, match it, call E(), then match )
Else → error
If all characters are parsed and pointer reaches \0, print Accepted.
Otherwise print Rejected.
End
Code:
Lab Sheet 9
Write a C program to implement LALR parsing.
Objective
To implement a LALR (Look-Ahead LR) parser in C language for a given context-free grammar,
using a predefined LALR parsing table (manually created or generated via tools like
YACC/Bison), and simulate how a shift-reduce parser works with lookahead.
Algo:
Start
Read the input string and append $ at the end.
Initialize the stack with "0" (starting state).
Set input pointer ip = 0.
Repeat:
Let state = top of stack (last digit).
Let a = current input symbol.
Find the ACTION [state, a] from the action table.
If ACTION is Shift (Sx):
Push the symbol a onto stack
Push the state x onto stack
Move input pointer to next symbol
Else if ACTION is Reduce (Ry):
Apply production number y
Pop symbols from stack based on production
Push the left-hand non-terminal
Use GOTO table to find next state and push it
Else if ACTION is ACC:
Print "Accepted"
Stop
Else:
Print "Rejected"
Stop
End
Code:
Lab Sheet 10
Write a C program to implement program semantic rules to calculate the
expression that takes an expression with digits, +, and * and computes the
value.
Objective:
The objective is to design a C program that parses and computes the value of arithmetic
expressions involving single-digit operands, the addition (+) and multiplication (*) operators,
and proper use of parentheses. The program should simulate semantic analysis by
associating semantic rules with grammar productions to compute the final result during
parsing. This mimics the behaviour of a syntax-directed translation scheme, a key concept in
compiler design for interpreting or compiling
arithmetic expressions.
Algo:
Start
Read the arithmetic expression as a string.
Call function E() to evaluate the expression.
E():
Compute value of T()
While next symbol is +,
Skip +
Add another T() to value
Return value
T():
Compute value of F()
While next symbol is *,
Skip *
Multiply by next F()
Return value
F():
If current character is a digit, convert it to integer and return it.
Print the final result.
End
Code: