Top Banner
[ Tutorial on AN -1424 : A Parser Generation Tool
16

Tutorial on AN

Jan 11, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tutorial on AN

[

Tutorial on AN-1424 :

A Parser Generation Tool

Page 2: Tutorial on AN

Assumptions

- Eclipse IDE- Installation of the ANTLR4 tool- Download the jar file of ANTLR4- Further References: * ANTLR website * Book on ANTLR4EEF

Page 3: Tutorial on AN

ANTLR (ANother Tool for Language Recognition)

ANTLR

- Parser.java- Lexer.java- Visitor classes

- Parser.java- Lexer.java

Your Java Classes

uses

uses

generates

generates

input file

output

Compiler

tee:bt

Page 4: Tutorial on AN

ANTLR (ANother Tool for Language Recognition)

ANTLR

- Parser.java- Lexer.java- Visitor classes

- Parser.java- Lexer.java

Your Java Classes

uses

uses

generates

generates

input file

output

Compiler I

÷÷i÷÷÷÷ Sot

Page 5: Tutorial on AN

Composite Pattern of Model Classes

Number

VariableDeclaration

Variable Multiplication

Addition

ExpressionProgram expressions: List<..>O

EEE

Page 6: Tutorial on AN

Composite Pattern of Model Classes

Number

VariableDeclaration

Variable Multiplication

Addition

ExpressionProgram expressions: List<..>

Page 7: Tutorial on AN

Building Model Objects from Parse Trees

i : INT = 5j : INT = 7i + j * 3

Variable

id

VariableDeclarationidtype

value

Multiplication

leftright

Additionleftright

Number

num

VariableDeclarationidtype

value

Variable

id

expressionsProgram 0 1 2

prog00'

EEFEEFEEE:*'

Page 8: Tutorial on AN

Building Model Objects from Parse Trees

i : INT = 5j : INT = 7i + j * 3

Variable

id

VariableDeclarationidtype

value

Multiplication

leftright

Additionleftright

Number

num

VariableDeclarationidtype

value

Variable

id

expressionsProgram 0 1 2

progi÷÷÷¥÷¥¥÷÷÷÷: FEE off,

Page 9: Tutorial on AN

Building Model Objects from Parse Trees

i : INT = 5j : INT = 7i + j * 3

Variable

id

VariableDeclarationidtype

value

Multiplication

leftright

Additionleftright

Number

num

VariableDeclarationidtype

value

Variable

id

expressionsProgram 0 1 2

prog

"*red BE

""!..¥¥d""

*oh¥:¥:¥i÷÷iiii÷e¥¥g÷¥:':iii¥eed¥%...

.⇐¥EET

'

Page 10: Tutorial on AN

ExprVisitor<T>*

ExprBaseVisitor<T>+

AntlrToProgram+ AntlrToExpression+

Hierarchy of Visitor Classes

implements

extends extends

o

ty

Page 11: Tutorial on AN

ExprVisitor<T>*

ExprBaseVisitor<T>+

AntlrToProgram+ AntlrToExpression+

Hierarchy of Visitor Classes

implements

extends extends

a.ir#qOoexeenaa.y:7.EIhcFare*ediI7r"

..

'

Page 12: Tutorial on AN

Building Model Objects from Parse Trees

i : INT = 5j : INT = 7i + j * 3

Variable

id

VariableDeclarationidtype

value

Multiplication

leftright

Additionleftright

Number

num

VariableDeclarationidtype

value

Variable

id

expressionsProgram 0 1 2

prog

O t÷'

*⇒÷¥:i¥÷÷÷÷÷STO

Page 13: Tutorial on AN

Syntax Errors vs. Semantic Errorsgonna

§ .

→ typeerror

XII - syntax errors must be

pks④ all fixed bets sematicend errors can be reported

.

Page 14: Tutorial on AN

Building Model Objects from Parse Trees

i : INT = 5j : INT = 7i + j * 3

Variable

id

VariableDeclarationidtype

value

Multiplication

leftright

Additionleftright

Number

num

VariableDeclarationidtype

value

Variable

id

expressionsProgram 0 1 2

prog

O

⇒¥¥ii÷¥¥¥⇒:*

Page 15: Tutorial on AN

Visitor Action

grammar vs. semantic actions

retrievalofmodelobject

Visitor vs. Action

distributed centralized

indirect(visit parse tree)

direct(built as parsing goes)It

Page 16: Tutorial on AN

Visitor Action

grammar vs. semantic actions

retrievalofmodelobject

Visitor vs. Action

distributed centralized

indirect(visit parse tree)

direct(built as parsing goes)

→iii

parrot"[email protected];'m[

Program objmdonesequ.de annually.