language translator - wordpress.com · translate the hll program compatible with m/c a, in to the...
TRANSCRIPT
![Page 1: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/1.jpg)
Pune Vidyarthi Griha’s
COLLEGE OF ENGINEERING, NASHIK.
“ LANGUAGE TRANSLATOR ”
By
Prof. Anand N. Gharu (Assistant Professor)
PVGCOE Computer Dept.
22nd Jan 2018 .
![Page 2: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/2.jpg)
CONTENTS :-
1. Role of lexical analysis
2. Parsing, token, pattern, lexemes lex. Error
3. Regular def. for language construct & string
4. Sequences, comments & transition diagram for
recognition of tokens, reserved word & ident.
5. Introduction to Compiler & Interpreters
6. General model of Compiler
7. Compare compiler and interpreter
8. Use of interpreter & component of interpreter
9. Overview of Lex & YACC Specifications.
![Page 3: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/3.jpg)
3
What’s a compiler? • All computers only understand machine language
• Therefore, high-level language instructions must be translated into machine language prior to execution
10000010010110100100101……
This is
a program
![Page 4: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/4.jpg)
4
What’s a compiler? • Compiler
A piece of system software that translates high-level languages into machine language
10000010010110100100101……
Congrats!
while (c!='x')
{
if (c == 'a' || c == 'e' || c == 'i')
printf("Congrats!");
else
if (c!='x')
printf("You Loser!");
}
Compiler
gcc -o prog program.c
program.c
prog
![Page 5: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/5.jpg)
Compiler • Complier:-
• These are the system programs which will
automatically translate the High level language
program in to the machine language program
Source program High level Lang. Prog.
Target program / M/C Lang. Prog.
Compiler
Database
![Page 6: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/6.jpg)
Types of Compiler • Cross Assembler:-
• These are the system programs which will automatically
translate the Assembly Language program compatible with
M/C A, in to the machine language program compatible with
M/C A
Cross Assembler
Source program Assembly Lang. Prog. Compatible with M/C A
Target program / M/C Lang. Prog. Compatible with M/C A
M/C B
![Page 7: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/7.jpg)
Types of compiler • Cross Compiler:-
• These are the system programs which will automatically
translate the HLL program compatible with M/C A, in to the
machine language program compatible with M/C A , but the
underlying M/C is M/C B
Cross Compiler
Source program
HLL Prog. Compatible with M/C A
Target program / M/C Lang. Prog.
M/C B
![Page 8: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/8.jpg)
Types of Compiler
![Page 9: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/9.jpg)
![Page 10: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/10.jpg)
Interpreter - It is the language translator which execute source program line by line with out translating them into machine language.
- It does not generate object code.
![Page 11: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/11.jpg)
Compiler vs Interpreter
, C++ , Visual Basic
![Page 12: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/12.jpg)
Phases of compiler
![Page 13: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/13.jpg)
13
• Any compiler must perform two major tasks
o Analysis of the source program
o Synthesis of a machine-language program
Structure of Compiler
Compiler
Analysis Synthesis
![Page 14: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/14.jpg)
Structure of Compiler
14
Scanner Parser Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all Phases of The Compiler)
(Character Stream)
Intermediate
Representation
Target machine code
![Page 15: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/15.jpg)
Structure of Compiler
15
Scanner Parser Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Scanner (Lexical Analysis) The scanner begins the analysis of the source program
by reading the input, character by character, and
grouping characters into individual words and symbols (tokens)
RE ( Regular expression )
NFA ( Non-deterministic Finite Automata )
DFA ( Deterministic Finite Automata )
LEX
(Character Stream)
Intermediate
Representation
Target machine code
![Page 16: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/16.jpg)
Structure of Compiler
16
Scanner Parser Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Parser (Syntax Analysis) Given a formal syntax specification (typically as a
context-free grammar [CFG] ), the parse reads tokens and groups them into units as specified by the productions of the CFG being used.
As syntactic structure is recognized, the parser either calls corresponding semantic routines directly or builds a syntax tree.
CFG ( Context-Free Grammar )
BNF ( Backus-Naur Form )
GAA ( Grammar Analysis Algorithms )
LL, LR, SLR, LALR Parsers
YACC
(Character Stream)
Intermediate
Representation
Target machine code
![Page 17: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/17.jpg)
Structure of Compiler
17
Scanner Parser Semantic
Routines
Code
Generator
Optimizer
Source
Program
(Character Stream)
Tokens Syntactic
Structure
Intermediate
Representation
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Semantic Routines Perform two functions
Check the static semantics of each construct
Do the actual translation
The heart of a compiler
Syntax Directed Translation
Semantic Processing Techniques
IR (Intermediate Representation) Target machine code
![Page 18: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/18.jpg)
Structure of Compiler
18
Scanner Parser Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all
Phases of
The Compiler)
Optimizer The IR code generated by the semantic routines is
analyzed and transformed into functionally equivalent but improved IR code
This phase can be very complex and slow
Peephole optimization
loop optimization, register allocation, code scheduling
Register and Temporary Management
Peephole Optimization
(Character Stream)
Intermediate
Representation
Target machine code
![Page 19: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/19.jpg)
Structure of Compiler
19
Source
Program
(Character Stream)
Scanner Tokens
Parser Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Code Generator Interpretive Code Generation
Generating Code from Tree/Dag
Grammar-Based Code Generator
Target machine code
![Page 20: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/20.jpg)
Structure of Compiler
20
Scanner [Lexical Analyzer]
Parser [Syntax Analyzer]
Semantic Process [Semantic analyzer]
Code Generator
[Intermediate Code Generator]
Code Optimizer
Tokens
Parse tree
Abstract Syntax Tree w/ Attributes
Non-optimized Intermediate Code
Optimized Intermediate Code
Code Optimizer
Target machine code
![Page 21: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/21.jpg)
Lexical Analysis
![Page 22: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/22.jpg)
Syntax Analysis
![Page 23: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/23.jpg)
Semantic Analysis
![Page 24: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/24.jpg)
Intermediate Code Generation
![Page 25: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/25.jpg)
![Page 26: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/26.jpg)
![Page 27: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/27.jpg)
![Page 28: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/28.jpg)
![Page 29: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/29.jpg)
![Page 30: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/30.jpg)
![Page 31: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/31.jpg)
Code Optimization
![Page 32: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/32.jpg)
Code Generation
![Page 33: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/33.jpg)
![Page 34: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/34.jpg)
![Page 35: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/35.jpg)
![Page 36: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/36.jpg)
![Page 37: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/37.jpg)
![Page 38: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/38.jpg)
![Page 39: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/39.jpg)
![Page 40: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/40.jpg)
![Page 41: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/41.jpg)
![Page 42: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/42.jpg)
Code Optimization
![Page 43: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/43.jpg)
![Page 44: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/44.jpg)
![Page 45: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/45.jpg)
![Page 46: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/46.jpg)
![Page 47: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/47.jpg)
![Page 48: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/48.jpg)
![Page 49: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/49.jpg)
![Page 50: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/50.jpg)
![Page 51: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/51.jpg)
![Page 52: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/52.jpg)
Structure of Compiler
Compiler writing tools
• Compiler generators or compiler-
compilers
oE.g. scanner and parser
generators
oExamples : Yacc, Lex
52
![Page 53: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/53.jpg)
Overview of Lex & YAAC
Lex:
Theory.
Execution.
Example.
Yacc:
Theory.
Description.
Example.
Lex & Yacc linking.
Demo.
53
![Page 54: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/54.jpg)
Lex
lex is a program (generator) that generates lexical analyzers, (widely
used on Unix).
It is mostly used with Yacc parser generator.
Written by Eric Schmidt and Mike Lesk.
It reads the input stream (specifying the lexical analyzer ) and
outputs source code implementing the lexical analyzer in the C
programming language.
Lex will read patterns (regular expressions); then produces C code
for a lexical analyzer that scans for identifiers.
54
![Page 55: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/55.jpg)
STRUCTURE OF LEX
![Page 56: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/56.jpg)
Lex
◦ A simple pattern: letter(letter|digit)*
Regular expressions are translated by lex to a computer
program that mimics an FSA.
This pattern matches a string of characters that begins with a
single letter followed by zero or more letters or digits.
56
![Page 57: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/57.jpg)
Lex
Some limitations, Lex cannot be used to recognize nested
structures such as parentheses, since it only has states and
transitions between states.
So, Lex is good at pattern matching, while Yacc is for more
challenging tasks. 57
![Page 58: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/58.jpg)
Lex
Pattern Matching Primitives
58
![Page 59: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/59.jpg)
Lex
• Pattern Matching examples.
59
![Page 60: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/60.jpg)
Lex ……..Definitions section……
%%
……Rules section……..
%%
……….C code section (subroutines)……..
• The input structure to Lex.
•Echo is an action and
predefined macro in lex that
writes code matched by the
pattern.
60
![Page 61: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/61.jpg)
Lex
Lex predefined variables.
61
![Page 62: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/62.jpg)
Lex
Whitespace must separate the defining term and the associated expression.
Code in the definitions section is simply copied as-is to the top of the generated
C file and must be bracketed with “%{“ and “%}” markers.
substitutions in the rules section are surrounded by braces ({letter}) to
distinguish them from literals. 62
![Page 63: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/63.jpg)
Yacc Theory:
◦ Yacc reads the grammar and generate C code for a parser .
◦ Grammars written in Backus Naur Form (BNF) .
◦ BNF grammar used to express context-free languages .
◦ e.g. to parse an expression , do reverse operation( reducing the
expression)
◦ This known as bottom-up or shift-reduce parsing .
◦ Using stack for storing (LIFO).
63
![Page 64: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/64.jpg)
STRUCTURE OF YACC
![Page 65: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/65.jpg)
Yacc
• Input to yacc is divided into three sections.
... definitions ...
%%
... rules ...
%%
... subroutines ...
65
![Page 66: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/66.jpg)
Yacc
The definitions section consists of:
◦ token declarations .
◦ C code bracketed by “%{“ and
“%}”.
◦ the rules section consists of:
BNF grammar .
the subroutines section consists of:
◦ user subroutines .
66
![Page 67: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/67.jpg)
yacc& lex in Together
• The grammar:
program -> program expr | ε
expr -> expr + expr | expr - expr | id
• Program and expr are nonterminals.
• Id are terminals (tokens returned by lex) .
• expression may be :
o sum of two expressions .
o product of two expressions .
o Or an identifiers
67
![Page 68: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/68.jpg)
Lex file
68
![Page 69: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/69.jpg)
Yacc file
69
![Page 70: LANGUAGE TRANSLATOR - WordPress.com · translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B](https://reader033.vdocument.in/reader033/viewer/2022052718/5f04a4937e708231d40efc97/html5/thumbnails/70.jpg)
Linking lex&yacc
70