ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.

Download ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.

Post on 27-Dec-2015

215 views

Category:

Documents

3 download

TRANSCRIPT

  • Slide 1
  • ICS611 Introduction to Compilers Set 1
  • Slide 2
  • What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine language A simple representation would be: Source Code ----> Compiler -----> Machine Language (Object File) It's not as simple as an assembler translator. A compiler has to perform several steps to produce an object file in machine code form.
  • Slide 3
  • Analysis of the source code Lexical Analysis using a program called a SCANNER Syntax Analysis using a program called a PARSER Semantic Analysis using CODE GENERATION routines
  • Slide 4
  • Lexical Analysis Scan the input source code to identify tokens of the programming language. Tokens are basic units (keywords, identifier names, etc.) from which the source code is constructed The program to do this is called a SCANNER
  • Slide 5
  • Syntax Analysis Group the tokens identified by the scanner into grammatical phrases that will be used by the compiler to generate the output code. This process is called parsing and is performed by a parser.
  • Slide 6
  • Synthesis of the target program Generate an intermediate representation of the source program Code Optimization Code Generation
  • Slide 7
  • Generate an intermediate representation This is performed by some compilers, but not necessarily all. It should be easy to produce and easy to translate into the target program.
  • Slide 8
  • Code Optimization Improve the intermediate code to produce a faster running machine code in the final translation. Not all compilers include the code optimization step, which can require a lot of time.
  • Slide 9
  • Linking the programs produced by the compiler The linker program links the object files from the program modules, and any additional library files to create the executable program. This requires the use of relocatable addresses within the program to allow the program to run in different memory locations.
  • Slide 10
  • Grammar 1 1. SENTENCE -> NOUNPHRASE VERB NOUNPHRASE 2. NOUNPHRASE -> the ADJECTIVE NOUN 3. NOUNPHRASE -> the NOUN 4. VERB -> pushed 5. VERB -> helped 6. ADJECTIVE -> pretty 7. ADJECTIVE -> poor 8. NOUN -> man 9. NOUN -> boy 10. NOUN -> cat
  • Slide 11
  • Grammar 1 is an example of a context-free grammar (the only kind we will deal with). The grammar consist of 10 productions e.g. production 3 is nounphrase -> the noun Here nounphrase is referred to as the lefthand side (lhs) and the noun is referred to as the righthand side (rhs)
  • Slide 12
  • The set of all lhss constitutes the set of nonterminals of the grammar. In this case they are: {SENTENCE, NOUNPHRASE,VERB, ADJECTIVE, NOUN} All the other symbols occurring in the grammar (i.e. in some rhs, but never as any lhs) are the terminals of the grammar. In this case {the,pushed,helped,pretty,poor,}
  • Slide 13
  • The lhs of the first production is called the goal symbol, in this case sentence. A derivation of a string in the grammar is a list of strings starting with the goal symbol, in which each string, except the first, is obtained from the preceding one by applying a substitution of one of its symbols using one of the productions as a substitution rule
  • Slide 14
  • A string which has a derivation is said to be derivable. Derivable strings that consist entirely of terminal symbols are called sentences of the grammar. E.g. the man helped the poor boy is a sentence of Grammar 1. The set of all sentences of a grammar is called the language defined by the grammar
  • Slide 15 NOUNPHRASE VERB NOUNPHRASE (by Rule 1) 3"> NOUNPHRASE VERB NOUNPHRASE (by Rule 1) 3. ==> the NOUN VERB NOUNPHRASE (Rule 3) 4. ==> the man VERB NOUNPHRASE (Rule 8) 5. ==> the man helped NOUNPHRASE 6. ==> the man helped the ADJECTIVE NOUN 7. ==> the man helped the poor NOUN 8. ==> the man helped the poor boy (this derivation shows that "the man helped the poor boy is a sentence in the language defined by the grammar.)"> NOUNPHRASE VERB NOUNPHRASE (by Rule 1) 3" title="Grammar 1 (Cont.1) Derivation of the sentence: "the man helped the poor boy 1. SENTENCE (goal symbol) 2. ==> NOUNPHRASE VERB NOUNPHRASE (by Rule 1) 3">
  • Grammar 1 (Cont.1) Derivation of the sentence: "the man helped the poor boy 1. SENTENCE (goal symbol) 2. ==> NOUNPHRASE VERB NOUNPHRASE (by Rule 1) 3. ==> the NOUN VERB NOUNPHRASE (Rule 3) 4. ==> the man VERB NOUNPHRASE (Rule 8) 5. ==> the man helped NOUNPHRASE 6. ==> the man helped the ADJECTIVE NOUN 7. ==> the man helped the poor NOUN 8. ==> the man helped the poor boy (this derivation shows that "the man helped the poor boy is a sentence in the language defined by the grammar.)
  • Slide 16
  • Grammar 1 (Cont.2) This derivation may also be represented diagrammatically by a syntax tree:
  • Slide 17
  • Typical format of a grammar for a programming language PROGRAM -> PROGRAM STATEMENT PROGRAM -> STATEMENT STATEMENT -> ASSIGNMENT-STATEMENT STATEMENT -> IF-STATEMENT STATEMENT -> DO-STATEMENT... ASSIGNMENT-STATEMENT ->...... IF-STATEMENT ->...... DO-STATEMENT ->......
  • Slide 18
  • Grammar 2 A simple grammar for arithmetic statements 1. E -> E + T 2. E -> T 3. T -> T * a 4. T -> a
  • Slide 19
  • Grammar 2 (Cont.1) Derivation of: a + a * a 1. E Goal Symbol 2. ==> E + T Rule 1 3. ==> E + T * a Rule 3 4. ==> E + a * a Rule 4 5. ==> T + a * a Rule 2 6. ==> a + a * a Rule 4
  • Slide 20
  • Grammar 2 (Cont.2) Derivation of: a + a * a written in reverse: 1. a + a * a Given sentential form 2. T + a * a Rule 4 in reverse 3. E + a * a Rule 2 in reverse 4. E + T * a Rule 4 5. E + T Rule 3 in reverse 6. E Rule 1

Recommended

View more >