0% found this document useful (0 votes)
36 views

Course 1

This document discusses the course "Formal Languages and Compiler Design" taught by Simona Motogna. It covers topics like the structure of a compiler, scanning (lexical analysis), parsing, semantic analysis, intermediate code generation, optimization, and code synthesis. Scanning involves detecting tokens in source code, classifying them, and codifying them in a program internal form and symbol table. The course aims to help students become better programmers by learning performant algorithms and understanding compilers. Students must meet minimum attendance and grade requirements to pass.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Course 1

This document discusses the course "Formal Languages and Compiler Design" taught by Simona Motogna. It covers topics like the structure of a compiler, scanning (lexical analysis), parsing, semantic analysis, intermediate code generation, optimization, and code synthesis. Scanning involves detecting tokens in source code, classifying them, and codifying them in a program internal form and symbol table. The course aims to help students become better programmers by learning performant algorithms and understanding compilers. Students must meet minimum attendance and grade requirements to pass.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Formal Languages and

Compiler Design

Simona Motogna

S. Motogna - LFTC
Compiler
Historical reasons
Formal Design
Languages

Be a better programmer

Performant algorithms
FLCD

S. Motogna - LFTC
Organization Issues

• Course – 2 h/ week 10 presences – seminar


• Seminar – 2h/week 12 presences - lab
• Laboratory - 2 h/week

PRESENCE IS MANDATORY

S. Motogna - LFTC
Most interesting stuff for students
• Moodle:
• All course resources
• Homeworks
• Assignments
• Labs
• Points / grades

• MsTeams – labs (maybe)

S. Motogna - LFTC
Minimal Conditions to Pass

• Minimum 10 presences at seminar


• Minimum 12 presences at laboratory

• Minimum grade 6 at lab


• Minimum grade 5 at final exam
Final grade

60% final exam


+
30% lab
+
10% seminar

Bonus
Lab work

• 10 laboratory tasks

• !!! Must be completed and loaded during lab hours

• Weighted grades:
Lab grade
Bonus points:
- “awesome” solutions
- Extra work
I wish …

Effective communication Interactive experience Learning fun


References
• See fișa disciplinei

S. Motogna - LFTC
S. Motogna - LFTC
What is a compiler?
Interpreter?

Source code / Object code /


program program
Compiler
Assembler?

S. Motogna - LFTC
S. Motogna - LFTC
A little bit of history …

Java
1995
C
Pascal J. Gosling
1969 - 1973
1968 - 1970 D. Ritchie
Lisp
N. Wirth
1962
Fortran McCarthy
1954-1957
Backus
S. Motogna - LFTC
Structure of a compiler Take
notes!
Source code/
analysis Error handling
Scanning (lexical
program analysis)

Parsing (syntactical
Tokens & ST analysis)

Syntax tree Semantic analysis

Intermediary code
Adnotated syntax generation synthesis
tree
Intermediary
Symbol Table Intermediary code optimization
management code
Object code /
Optimized Object code program
generation
intermediary code

S. Motogna - LFTC
Chapter 1. Scanning
Definition = treats the source program as a sequence of characters,
detect lexical tokens, classify and codify them

INPUT: source program


OUTPUT: PIF + ST

Algorithm Scanning v1
While (not(eof)) do
detect(token);
classify(token);
codify(token);
End_while

S. Motogna - LFTC
Take

Detect notes!

I am a student.I am
Simona

- Separators => Remark 1)

if (x==y) {x=y+2}

- Look-ahead => Remark 2)

S. Motogna - LFTC
Classify
• Classes of tokens:
• Identifiers
• Constants
• Reserved words (keywords)
• Separators
• Operators

• If a token can NOT be classified => LEXICAL ERROR

S. Motogna - LFTC
Codify
• May be codification table
OR
code for identifiers and constants

• Identifier, constant => Symbol Table (ST)

• PIF = Program Internal Form = array of pairs

• pairs (token, position in ST)

identifier, constant

S. Motogna - LFTC
Algorithm Scanning v2
While (not(eof)) do
detect(token);
if token is reserved word OR operator OR separator
then genPIF(token, 0)
else a=a+b

if token is identifier OR constant FIP


(id,1)
then index = pos(token, ST); (=,0)
(id,1)
genPIF(token, index) (+,0)
else message “Lexical error” (id,2)

endif ST
1 a
endif 2 b
endwhile
S. Motogna - LFTC
Remarks:
• genPIF = adds a pair (token, position) to PIF

• Pos(token,ST) – searches token in symbol table ST; if found then


return position; if not found insert in SR and return position
• Order of classification (reserved word, then identifier)
• If-then-else imbricate => detect error if a token cannot be classified

S. Motogna - LFTC
Example (sem?)
• https://babeljs.io/docs/en/
• https://www.antlr.org/ and https://github.com/antlr/antlr4
• https://www.programiz.com/python-programming/online-compiler/
• https://www.w3schools.com/python/python_compiler.asp

S. Motogna - LFTC

You might also like