Compiler Design ECX6235 Answers For Tma 01: Name Reg. No Centre Due Date
Compiler Design ECX6235 Answers For Tma 01: Name Reg. No Centre Due Date
ECX6235
ANSWERS FOR TMA 01
REG. No : 511078618
CENTRE : COLOMBO
(a)
• P is Production rules for Terminals and Non-terminals. A production rule has the form α
→ β, where α and β are strings on VN ∪ ∑ and least one symbol of α belongs to VN.
Types of grammars,
Lexical Analysis
In the lexical analysis phase, the compiler scans the characters of the source program, one
character at a time. Whenever it gets a sufficient number of characters to constitute a token of
the specified language, it outputs that token. In order to perform this task, the lexical analyzer
must know the keywords, identifiers, operators, delimiters, and punctuation symbols of the
language to be implemented. So, when it scans the source program, it will be able to return a
suitable token whenever it encounters a token lexeme.
The lexical analyzer identify the each identifiers(id) by using regular expressions.
Syntax analysis or parsing is the second phase of a compiler. The parser analyzes the source
code (token stream) against the production rules to detect any errors in the code. The output of
this phase is a parse tree. Syntax analyser get all the tokens one by one and take the grammar
Parsers are expected to parse the whole code even if some errors exist in the program.
Parsers use error recovering strategies.
Semantic Analyzer
Compilers generate an explicit intermediate code representation of the source program. The
intermediate code can have a variety of forms. For example, a three-address code.
Code Optimization
In the optimization phase, the compiler performs various transformations in order to improve
the intermediate code. These transformations will result in faster-running machine code.
Code Generation
The final phase in the compilation process is the generation of target code. This process
involves selecting memory locations for each variable used by the program. Then, each
intermediate instruction is translated into a sequence of machine instructions that performs the
same task.
(c)
(a)
c. Production rules are,
S while <NT1><NT3>
<NT1> (<NT2>)
<NT2> id(x)[<]c(7)
<NT3> {NT4}
<NT4> <NT5>;<NT6>;
<NT5> print. Id(x)
<NT6> id(x)[=]<NT7>
<NT7> id(x) [+] c(1)
Let’s take
while as w
; as a
( as b
) as d
id(x) as i
[] as o
c(x) as c
{ as f
} as g
Print as p
NTx as N
T = {w, b, d, i, e, c, f, g, p, h, i}
S = wNN
P = {S wNN, N bNd,N ioc, N fNg, N NaNa, N pi, N ioN}
S wNN
S wbNdN
S wbiocdN
S wbiocdfNg
S wbiocdfNaNag
S wbiocdfpiaNag
S wbiocdfgpiaioNag
S wbiocdfgpiaioiocag
S wbiocdfgpia(io)+cag
[Q3]
a)
b)
Grammar
Production rules
START → SWITCH
SWITCH → LAMP on | LAMP off
LAMP → lamp1 | lamp2
String L1 on
START → SWITCH
→ L on
→ L1 on
c)
DFA
L1 on
NDFA
L2 on
L
[Q4]
Lexical Analysis
/* Lexical Analysis */
%{
#include "y.tab.h"
#include <stdlib.h>
void yyerror(char *);
%}
%%
START { return(START); }
SWITCH{ return(SWITCH); }
LAMP { return(LAMP); }
lamp1 { return(k); }
lamp2 { return(k); }
. yyerror("Unknown character");
%%
int yywrap(void) {
return 1;
}
Syntax Analysis
/* Syntax Analysis */
%{
#include <stdio.h> /* For I/O */
int errors; /* Error Count */
// Terminals
%token START
%token SWITCH
%token LAMP
%token lamp1
%token lamp2
%%
%%
PROGRAM:
STATUS lamp1 on;
STATUS:
status {printf(" obtained current status \n");}
COMP:
lamp1 {printf("obtained lamp status ");}
|lamp2 {printf("obtained fan status");}
%%