CD
CD
Tokens are the smallest units in a program like int, main, =, +, ; which are classified as
keywords, identifiers, operators, punctuation, etc.
3. What is the role of spaces and delimiters in lexical analysis?
Spaces and delimiters (like ,, ;, (, )) help to separate tokens and signal where one ends and
another begins.
4. How does a lexer differentiate between keywords and identifiers?
It uses a lookup or predefined keyword list. If a token matches a keyword, it’s classified as a
keyword; otherwise, it’s an identifier.
5. Why is lexical analysis important in compilation?
It simplifies syntax analysis by converting raw code into a stream of tokens, removing
comments and whitespaces.
Int main() {
Char input[100];
Printf(“Enter code line: “);
Fgets(input, 100, stdin);
📌 Sample Output:
Would you like me to continue like this for Practical 2: FIRST and FOLLOW next?
Here are the most commonly asked viva questions (with answers) and compact but input-
based functional code for each of your 7 compiler design practicals from dpaste.org/8JPwA:
✅ 1. Lexical Analyzer
Top Viva Questions:
1. What is a lexical analyzer?
o It scans the source code and breaks it into tokens.
2. What is a token?
o A keyword, identifier, operator, or literal recognized by the
lexer.
3. What tools can be used to build lexical analyzers?
o Lex, JFlex, custom C/Java programs.
4. Difference between Lexeme and Token?
o Lexeme is the actual input string; Token is its classification.
5. What is the role of a symbol table in lexical analysis?
o It stores information about identifiers.
Compact Code with Input:
#include<stdio.h>
#include<ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (isalpha(ch)) printf("Identifier\n");
else if (isdigit(ch)) printf("Digit\n");
else if (ch == '+' || ch == '-') printf("Operator\n");
else printf("Unknown Symbol\n");
return 0;
}
✅ 7. Code Optimization
Top Viva Questions:
1. What is code optimization?
o Improving intermediate or final code.
2. What is dead code?
o Code whose result is never used.
3. What is common subexpression elimination?
o Reusing previously computed expressions.
4. Why optimize at IR level?
o Easier and more effective than machine code.
5. What are strength reductions?
o Replacing expensive operations (e.g., * by +).
Compact Code with Input:
#include<stdio.h>
int main() {
int a = 3, b = 4, c, d;
c = a * b;
d = a * b; // common subexpression
printf("Eliminated duplicate: use c\n");
printf("c = %d\n", c);
return 0;
}