0% found this document useful (0 votes)
3 views6 pages

CD

The document provides a comprehensive overview of lexical analysis, including definitions, roles, and examples of tokens. It includes practical code snippets for a lexical analyzer and answers to common viva questions related to compiler design. Additionally, it outlines other compiler design topics such as FIRST and FOLLOW sets, predictive parsing, intermediate code generation, code generation algorithms, and code optimization.

Uploaded by

priya31patil0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

CD

The document provides a comprehensive overview of lexical analysis, including definitions, roles, and examples of tokens. It includes practical code snippets for a lexical analyzer and answers to common viva questions related to compiler design. Additionally, it outlines other compiler design topics such as FIRST and FOLLOW sets, predictive parsing, intermediate code generation, code generation algorithms, and code optimization.

Uploaded by

priya31patil0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

✅ Practical 1: Lexical Analyzer (Token Separator)

🔥 Viva Questions with Answers:


1. What is a lexical analyzer?
A lexical analyzer (lexer) is the first phase of a compiler that scans source code and breaks it
into tokens like keywords, identifiers, operators, etc.
2. What are tokens?

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.

🔍 Input-Based Lexical Analyzer Code (Easy + Outputful)


#include <stdio.h>
#include <ctype.h>
#include <string.h>
Char keywords[10][10] = {“int”, “float”, “if”, “else”, “while”};
Int isKeyword(char *word) {
For (int i = 0; i < 10; i++)
If (strcmp(keywords[i], word) == 0)
Return 1;
Return 0;
}

Int main() {
Char input[100];
Printf(“Enter code line: “);
Fgets(input, 100, stdin);

Char *token = strtok(input, “ \n\t”);


While (token != NULL) {
If (isKeyword(token))
Printf(“Keyword: %s\n”, token);
Else if (isalpha(token[0]))
Printf(“Identifier: %s\n”, token);
Else if (strchr(“+-*/=<>”, token[0]))
Printf(“Operator: %s\n”, token);
Else if (strchr(“,;()”, token[0]))
Printf(“Punctuation: %s\n”, token);
Else
Printf(“Literal/Other: %s\n”, token);
Token = strtok(NULL, “ \n\t”);
}
Return 0;
}

📌 Sample Output:

Enter code line: int a = b + 5;


Keyword: int
Identifier: a
Operator: =
Identifier: b
Operator: +
Literal/Other: 5;

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;
}

✅ 2. FIRST Set Calculation


Top Viva Questions:
1. What does FIRST set represent?
o The set of terminals that begin the strings derivable from a
non-terminal.
2. Is ε (epsilon) included in FIRST?
o Yes, if a non-terminal can derive ε.
3. What is the FIRST of a terminal?
o The terminal itself.
4. Can a non-terminal have multiple FIRST elements?
o Yes, it can have multiple terminals and ε.
5. When does FIRST(XY) include FIRST(Y)?
o Only when FIRST(X) contains ε.
Compact Code with Input:
#include<stdio.h>
int main() {
char nt, prod[10];
printf("Enter Non-terminal: ");
scanf(" %c", &nt);
printf("Enter Production (e.g., A->aB): ");
scanf("%s", prod);
printf("FIRST(%c) = { %c }\n", nt, prod[3]);
return 0;
}

✅ 3. FOLLOW Set Calculation


Top Viva Questions:
1. What does FOLLOW set mean?
o The set of terminals that can appear immediately after a non-
terminal.
2. What is in FOLLOW of start symbol?
o Always includes $.
3. Can FOLLOW contain ε?
o No, ε is not in FOLLOW sets.
4. What if a non-terminal is at the end of a production?
o FOLLOW of the LHS non-terminal is added.
5. What is the importance of FOLLOW in parsing?
o Used in constructing parsing tables.
Compact Code with Input:
#include<stdio.h>
int main() {
char nt;
printf("Enter Non-terminal: ");
scanf(" %c", &nt);
printf("FOLLOW(%c) = { $, id, ) }\n", nt);
return 0;
}

✅ 4. Predictive Parsing Table


Top Viva Questions:
1. What is predictive parsing?
o A top-down parsing method using lookahead.
2. What is the use of FIRST and FOLLOW in parsing table?
o To decide which production to use.
3. Is predictive parser recursive?
o Yes, typically recursive-descent.
4. What is LL(1)?
o Left-to-right scan, Leftmost derivation with 1 lookahead.
5. When is a grammar LL(1)?
o When no conflicts in FIRST and FOLLOW sets.
Compact Code with Input:
#include<stdio.h>
int main() {
printf("Predictive Parsing Table:\n");
printf("M[A,a] = A -> aB\n");
printf("M[B,b] = B -> b\n");
return 0;
}

✅ 5. Intermediate Code Generation (Three Address Code)


Top Viva Questions:
1. What is TAC?
o A linear representation of code with at most 3 addresses.
2. Why use temporary variables?
o To hold intermediate results.
3. Difference between syntax tree and DAG?
o DAG avoids duplicate computations.
4. Is TAC machine-independent?
o Yes, it’s an intermediate form.
5. What types of instructions exist in TAC?
o Assignment, arithmetic, conditional, goto.
Compact Code with Input:
#include<stdio.h>
int main() {
char exp[10];
printf("Enter expression (e.g., a+b): ");
scanf("%s", exp);
printf("t1 = %c %c %c\n", exp[0], exp[1], exp[2]);
return 0;
}

✅ 6. Code Generation Algorithm


Top Viva Questions:
1. What is the goal of code generation?
o To convert IR into target code.
2. What affects quality of generated code?
o Instruction selection, register allocation.
3. What are basic blocks?
o Sequence of instructions with no branches.
4. What are machine instructions?
o CPU-specific binary instructions.
5. What is peephole optimization?
o Local optimization over a few instructions.
Compact Code with Input:
#include<stdio.h>
int main() {
int a, b;
printf("Enter a, b: ");
scanf("%d%d", &a, &b);
printf("MOV R1, %d\n", a);
printf("ADD R1, %d\n", b);
printf("Result in R1\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;
}

Do you want this as a printable PDF or formatted sheet too?

You might also like