CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical...
Transcript of CONSTRUCTION COMPILERSmath.uni.lodz.pl/~robpleb/Wyklad_1_ang.pdf · 12. LEXICAL ANALYSIS Lexical...
CONSTRUCTION COMPILERS
LECTURE
Robert Plebaniak
The software platform
LINUX (it may not contain LLgen, then the installation from the page http://tack.sourceforge.net);
WINDOWS (then we can use Cygwin, this program we can found http://www.cygwin.com);
2
LEX and YACC GENERATORS
PCYACC - commercial version http://www.abxsoft.com
MKS LEX & YACC – commercial version http://www.mkssoftware.com
Bumble-Bee Parser Generator – commercial version http://www.bumblebeesoftware.com
6
Compilers
Defintion 1.Compiler is a program whose purpose is to
convert code written in a programming language (C, Ada, Java), called the source language on the equivalent code in another language, called the language of the output.
9
INTERPRETER
Definition 2.Interpreter – is a program which operates as a
compiler, except that it does not generate object code, but immediately follows the instructions contained in the source code of the program.
10
Stages of the compiler
The source program; Analysis; The intermediate representation; Synthesis; The resulting program;
11
STAGES OF COMPILATION
Analysis – at this stage, the compiler breaks down the source program into its constituent parts and then generates the intermediate representation.
Also at this stage, we have detect errors and possible informing them about the programmer.
12
LEXICAL ANALYSIS
Lexical analysis is also called scanning or line analysis. The main task of lexical analysis is a grouping of characters in the input stream in lexical symbols otherwise known as token's.
14
LEXICAL ANALYSIS
Tokens are different elementary component of the source language, eg .: number, keywords, operators and identifiers, parentheses, ...
15
ANALIZA LEKSYKALNA
Lexical analysis is also called scanning or line analysis. The main task of lexical analysis is a grouping of characters in the input stream in lexical symbols otherwise known as token's.
16
Exampe
There is a function on C++:
int name_fun (int s){
return s+s;}
17
Example
Tokens are different elementary component of the source language, eg .: number, keywords, operators and identifiers, parentheses, ...
18
Example
Leksems – they are strings that form lexical symbols;
Pattern – a "recipe" for the construction token: this may be a string underscores, small, large letters and digits;
Attributes – that is the lexical value, which sometimes occur with tokens.
19
EXAMPLE
))‘)’
„s”[ _a-zA-Z][ _a-zA-z0-9]*
sIDENTintintKWD_INT((‘(‘
name_fun
[ _a-zA-Z][ _a-zA-z0-9]*
name_funIDENTintintKWD_INT
ATTRIBPATTERNLEKSEM TOKEN
20
EXAMPLE
„s”[_a-zA-Z][_a-zA-z0-9]*
sIDENT
„s”[ _a-zA-Z][ _a-zA-z0-9]*
sIDENT
++OP_PLUS
returnreturnKWD_RET‘{‘‘{‘‘{‘
ATRYB.PATTERNLEKSEM TOKEN
21
EXAMPLE
}}‘}’;;‘;‘
ATRYB.PATTERNLEKSEM TOKEN
22
PARSING
Parsing is also called the analysis of hierarchical or “synktatyczną”. The task of parsing is to investigate whether lexical units make the correct design of a given programming language (eg.: if maintained is appropriate sequence of tokens)..
24
EXAMPLE
There is a function on C++:
int (int s) name_fun{ } + s ; s return
25
SEMANTIC ANALYSIS
Semantic analysis has the following tasks: - Checking program (source program) in terms
of compliance with the semantic definition of the source language, in which the program is written;
- Gathering information for the generation of intermediate code;
27
CODE OPTIMILIZATION
The primary purpose of optimization is to improve the efficiency of finally code. It may be carried out at all levels of the code, ie. At the source code level, intermediate and also resulting.
34
EXAMPLE
And here is how the intermediate code of our example after optimization:
i:=1
s:=10*i
@loop: if i>n goto @end
cout<<s
i:=i+1
s:=s+10
goto @loop
@end:
i:=1
@loop:
if i>n goto @end
s:=10*i
cout<<s
i:=i+1
goto @loop
@end: 35
EXAMPLE
Let's create a flow chart for mathematical functions signum.
39
EXAMPLE
Implementation of C ++ such a short program might look like this:
float signum (int a) {
if a<0 return -1; else if (a==0) return 0;
else return 1; }
40
Example - flow graph of functions Example - flow graph of functions signumsignum
x:=a
If x<0 goto B
If x>0 goto A B: y:=-1 goto C
y=0 goto C A: y:=1 goto C
C: STOP
RUN-TIME ENVIRONMENT- rules of visibilityRules of visibility define the way in which the
compiler interprets the references in the program to non-local names. There are two possibilities:
- rules of static visibility - text analysis program (C, Ada);
- rules of dynamic visibility - during the execution of the program (snobol, Lisp, APN)
43
EXAMPLELet's take a bit of code in C ++:
int k=1; main() {cout<<„the value of k is ”<<k<<endl;
{int k=30;cout<<„in the block the value of k is ”<<k<<endl;}
cout<<„again outside the block k is”<<k;} 44
EXAMPLE
And here is what appears on the screen as a result of the program:
the value of k is 1 in the blocks the value of k is 30
again outside the block k is 1
45
EXAMPLEAnd here's how you can be appealed to the curtained
global names in C ++
int k=1;main(){cout<<„the value of k is ”<<k<<endl;
{int k=30;cout<<„in the block the value of k is ”<<k<<„ and the global value of k is”<<::k<<endl;}
END
END OF FIRST LECTURE