computer languages - minijava - concrete & abstract...
TRANSCRIPT
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Computer Languagesminijava - Concrete & Abstract syntax
1 Parsing2 Syntactic categories3 Concrete syntax4 Abstract syntax5 Composite pattern
February 4
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
The compiler has to
Analyze the source code to understandwhat it means!
in the process of doing so it has toreject meaningless sources!
Generate code in a language for whicha machine exists.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
The compiler has to
Analyze the source code to understandwhat it means!
in the process of doing so it has toreject meaningless sources!
Generate code in a language for whicha machine exists.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
The compiler has to
Analyze the source code to understandwhat it means!
in the process of doing so it has toreject meaningless sources!
Generate code in a language for whicha machine exists.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
Compilersourcecode
machinecode
errors
Has to distinguish correct fromincorrect programs (has tounderstand!)
Has to generate correct machinecode!
Has to organize memory forvariables and instructions!
Has to agree with OS on theform of object code!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
Compilersourcecode
machinecode
errors
Has to distinguish correct fromincorrect programs (has tounderstand!)
Has to generate correct machinecode!
Has to organize memory forvariables and instructions!
Has to agree with OS on theform of object code!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
Compilersourcecode
machinecode
errors
Has to distinguish correct fromincorrect programs (has tounderstand!)
Has to generate correct machinecode!
Has to organize memory forvariables and instructions!
Has to agree with OS on theform of object code!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
Compilersourcecode
machinecode
errors
Has to distinguish correct fromincorrect programs (has tounderstand!)
Has to generate correct machinecode!
Has to organize memory forvariables and instructions!
Has to agree with OS on theform of object code!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of a compiler
Compilersourcecode
machinecode
errors
Has to distinguish correct fromincorrect programs (has tounderstand!)
Has to generate correct machinecode!
Has to organize memory forvariables and instructions!
Has to agree with OS on theform of object code!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
The Front End (analysis phase)
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The Scanner (lexical analyzer) transforms a sequence ofcharacters (source code) into a sequence of tokens: arepresentation of the lexemes of the language.
The Parser (syntactical analyzer) takes the sequence of tokensand generates a tree representation, the Abstract Syntax.
This tree is analyzed by the type checker and is then used togenerate the intermediate representation.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
The Front End (analysis phase)
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The Scanner (lexical analyzer) transforms a sequence ofcharacters (source code) into a sequence of tokens: arepresentation of the lexemes of the language.
The Parser (syntactical analyzer) takes the sequence of tokensand generates a tree representation, the Abstract Syntax.
This tree is analyzed by the type checker and is then used togenerate the intermediate representation.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
The Front End (analysis phase)
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The Scanner (lexical analyzer) transforms a sequence ofcharacters (source code) into a sequence of tokens: arepresentation of the lexemes of the language.
The Parser (syntactical analyzer) takes the sequence of tokensand generates a tree representation, the Abstract Syntax.
This tree is analyzed by the type checker and is then used togenerate the intermediate representation.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
The Front End (analysis phase)
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The Scanner (lexical analyzer) transforms a sequence ofcharacters (source code) into a sequence of tokens: arepresentation of the lexemes of the language.
The Parser (syntactical analyzer) takes the sequence of tokensand generates a tree representation, the Abstract Syntax.
This tree is analyzed by the type checker and is then used togenerate the intermediate representation.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Plan for today
Syntactic categories
What kind of phrases are used in aprogramming language? Whatcomponents do they have?
Concrete syntax
How do the phrases look like? Whatwords, separators, punctuations areused?
Abstract syntax
What data structure can be used tostore the program so that thestructure is evident?
Composite pattern
How can we implement thisdata structure in Java?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Plan for today
Syntactic categories
What kind of phrases are used in aprogramming language? Whatcomponents do they have?
Concrete syntax
How do the phrases look like? Whatwords, separators, punctuations areused?
Abstract syntax
What data structure can be used tostore the program so that thestructure is evident?
Composite pattern
How can we implement thisdata structure in Java?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Plan for today
Syntactic categories
What kind of phrases are used in aprogramming language? Whatcomponents do they have?
Concrete syntax
How do the phrases look like? Whatwords, separators, punctuations areused?
Abstract syntax
What data structure can be used tostore the program so that thestructure is evident?
Composite pattern
How can we implement thisdata structure in Java?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Plan for today
Syntactic categories
What kind of phrases are used in aprogramming language? Whatcomponents do they have?
Concrete syntax
How do the phrases look like? Whatwords, separators, punctuations areused?
Abstract syntax
What data structure can be used tostore the program so that thestructure is evident?
Composite pattern
How can we implement thisdata structure in Java?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Plan for today
Syntactic categories
What kind of phrases are used in aprogramming language? Whatcomponents do they have?
Concrete syntax
How do the phrases look like? Whatwords, separators, punctuations areused?
Abstract syntax
What data structure can be used tostore the program so that thestructure is evident?
Composite pattern
How can we implement thisdata structure in Java?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Example
class Small{public static void main(String args[]){
System.out.println("Hello "+args[0]);}
}
Example
resource small()string[10] usergetarg(1,user)write("Hello",user)
end
Example
import System
main =do (user:_) <- getArgs
print("Hello " ++ user)
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Example
class Small{public static void main(String args[]){
System.out.println("Hello "+args[0]);}
}
Example
resource small()string[10] usergetarg(1,user)write("Hello",user)
end
Example
import System
main =do (user:_) <- getArgs
print("Hello " ++ user)
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Example
class Small{public static void main(String args[]){
System.out.println("Hello "+args[0]);}
}
Example
resource small()string[10] usergetarg(1,user)write("Hello",user)
end
Example
import System
main =do (user:_) <- getArgs
print("Hello " ++ user)
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Example
class Small{public static void main(String args[]){
System.out.println("Hello "+args[0]);}
}
Example
resource small()string[10] usergetarg(1,user)write("Hello",user)
end
Example
import System
main =do (user:_) <- getArgs
print("Hello " ++ user)
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
What is a Program?
In Java and minijava: a class with a static main method!
and some class declarations!
In C and C++: a main function (a static method!)!
and some function or class declarations!
In other languages it could be something else.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
What is a Program?
In Java and minijava: a class with a static main method!
and some class declarations!
In C and C++: a main function (a static method!)!
and some function or class declarations!
In other languages it could be something else.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
What is a Program?
In Java and minijava: a class with a static main method!
and some class declarations!
In C and C++: a main function (a static method!)!
and some function or class declarations!
In other languages it could be something else.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
What is a Program?
In Java and minijava: a class with a static main method!
and some class declarations!
In C and C++: a main function (a static method!)!
and some function or class declarations!
In other languages it could be something else.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Declarations
What we use to buildabstractions: we give names toentities.
Expressions
Entities that have a value.Expressions usually have anassociated type.
Statements
Entities that change state.
Some programming languageshave no statements (Haskell). Insome languages statements andexpressions are mixed up (C).Different programming paradigmssupport different kinds of valuesand abstractions.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Declarations
What we use to buildabstractions: we give names toentities.
Expressions
Entities that have a value.Expressions usually have anassociated type.
Statements
Entities that change state.
Some programming languageshave no statements (Haskell). Insome languages statements andexpressions are mixed up (C).Different programming paradigmssupport different kinds of valuesand abstractions.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Declarations
What we use to buildabstractions: we give names toentities.
Expressions
Entities that have a value.Expressions usually have anassociated type.
Statements
Entities that change state.
Some programming languageshave no statements (Haskell). Insome languages statements andexpressions are mixed up (C).Different programming paradigmssupport different kinds of valuesand abstractions.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Declarations
What we use to buildabstractions: we give names toentities.
Expressions
Entities that have a value.Expressions usually have anassociated type.
Statements
Entities that change state.
Some programming languageshave no statements (Haskell). Insome languages statements andexpressions are mixed up (C).Different programming paradigmssupport different kinds of valuesand abstractions.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in programming languages
Declarations
What we use to buildabstractions: we give names toentities.
Expressions
Entities that have a value.Expressions usually have anassociated type.
Statements
Entities that change state.
Some programming languageshave no statements (Haskell). Insome languages statements andexpressions are mixed up (C).Different programming paradigmssupport different kinds of valuesand abstractions.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - declarations
class A {int x;...
public int f(){int y;...
}
...
}
classes
A class can be used as thetype of an object.
fields
A field is a global variableinside a class, stands for aplace in memory.
methods
A method can be used insteadof a statement or anexpression.
local variables
Local to a method, stands fora register or a place inmemory.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - declarations
class A {int x;...
public int f(){int y;...
}
...
}
classes
A class can be used as thetype of an object.
fields
A field is a global variableinside a class, stands for aplace in memory.
methods
A method can be used insteadof a statement or anexpression.
local variables
Local to a method, stands fora register or a place inmemory.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - declarations
class A {int x;...
public int f(){int y;...
}
...
}
classes
A class can be used as thetype of an object.
fields
A field is a global variableinside a class, stands for aplace in memory.
methods
A method can be used insteadof a statement or anexpression.
local variables
Local to a method, stands fora register or a place inmemory.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - declarations
class A {int x;...
public int f(){int y;...
}
...
}
classes
A class can be used as thetype of an object.
fields
A field is a global variableinside a class, stands for aplace in memory.
methods
A method can be used insteadof a statement or anexpression.
local variables
Local to a method, stands fora register or a place inmemory.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - declarations
class A {int x;...
public int f(){int y;...
}
...
}
classes
A class can be used as thetype of an object.
fields
A field is a global variableinside a class, stands for aplace in memory.
methods
A method can be used insteadof a statement or anexpression.
local variables
Local to a method, stands fora register or a place inmemory.
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - expressions
3 true falsexx+98*y<x*y*ya.f()new A()arr[39]
constants
identifiers
using operators
calling a method
object creation
array element
What are the values of these expressions?
What are the values of identifiers? Are theexpressions meaningful?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Syntactic categories in minijava - statements
x = a.f();arr[38]=x;System.out.println(x);if (x<y) max=y;
else max=x;while(x<y)
x=x+y;
assignment
array assign
output
conditional
loop
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar for minijava
goal → mainClass classDeclarationList
mainClass → CLASS ID ’{’ PUBLIC STATIC VOID MAIN ’(’STRING ’[’ ’]’ ID ’)’ ’{’ statement ’}’
classDeclarationList → classDeclarationList classDeclaration| ∈
classDeclaration → CLASS ID ’{’ varDeclarationListmetDeclarationList ’}’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar for minijava
goal → mainClass classDeclarationList
mainClass → CLASS ID ’{’ PUBLIC STATIC VOID MAIN ’(’STRING ’[’ ’]’ ID ’)’ ’{’ statement ’}’
classDeclarationList → classDeclarationList classDeclaration| ∈
classDeclaration → CLASS ID ’{’ varDeclarationListmetDeclarationList ’}’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar for minijava
goal → mainClass classDeclarationList
mainClass → CLASS ID ’{’ PUBLIC STATIC VOID MAIN ’(’STRING ’[’ ’]’ ID ’)’ ’{’ statement ’}’
classDeclarationList → classDeclarationList classDeclaration| ∈
classDeclaration → CLASS ID ’{’ varDeclarationListmetDeclarationList ’}’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar for minijava
goal → mainClass classDeclarationList
mainClass → CLASS ID ’{’ PUBLIC STATIC VOID MAIN ’(’STRING ’[’ ’]’ ID ’)’ ’{’ statement ’}’
classDeclarationList → classDeclarationList classDeclaration| ∈
classDeclaration → CLASS ID ’{’ varDeclarationListmetDeclarationList ’}’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Overview of the grammar
metDeclaration → PUBLIC type ID ’(’ parameters ’)’ ’{’varDeclarationList statementListRETURN exp ’}’
statement → ’{’ statementList ’}’| IF ’(’ exp ’)’ statement ELSE statement| WHILE ’(’ exp ’)’ statement| SYSTEMOUT ’(’ exp ’)’ ’;’| ID ’=’ exp ’;’| ID ’[’ exp ’]’ ’=’ exp ’;’
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Remarks
The grammar in the appendix ofthe book describes a small subsetof Java called minijava. Do notadd things to it! We will compileminijava!
When understanding thegrammar, start from the goal.But when implementing it injacc start with expressions, thenstatements and then thedeclarations!
Read in the jacc manual how todo lists, lists with separators, etc.
The grammar will have someconflicts. Test it a lot in order todecide whether you can let jaccsolve them or whether you willhave to modify the grammar!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Remarks
The grammar in the appendix ofthe book describes a small subsetof Java called minijava. Do notadd things to it! We will compileminijava!
When understanding thegrammar, start from the goal.But when implementing it injacc start with expressions, thenstatements and then thedeclarations!
Read in the jacc manual how todo lists, lists with separators, etc.
The grammar will have someconflicts. Test it a lot in order todecide whether you can let jaccsolve them or whether you willhave to modify the grammar!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Remarks
The grammar in the appendix ofthe book describes a small subsetof Java called minijava. Do notadd things to it! We will compileminijava!
When understanding thegrammar, start from the goal.But when implementing it injacc start with expressions, thenstatements and then thedeclarations!
Read in the jacc manual how todo lists, lists with separators, etc.
The grammar will have someconflicts. Test it a lot in order todecide whether you can let jaccsolve them or whether you willhave to modify the grammar!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Remarks
The grammar in the appendix ofthe book describes a small subsetof Java called minijava. Do notadd things to it! We will compileminijava!
When understanding thegrammar, start from the goal.But when implementing it injacc start with expressions, thenstatements and then thedeclarations!
Read in the jacc manual how todo lists, lists with separators, etc.
The grammar will have someconflicts. Test it a lot in order todecide whether you can let jaccsolve them or whether you willhave to modify the grammar!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Remarks
The grammar in the appendix ofthe book describes a small subsetof Java called minijava. Do notadd things to it! We will compileminijava!
When understanding thegrammar, start from the goal.But when implementing it injacc start with expressions, thenstatements and then thedeclarations!
Read in the jacc manual how todo lists, lists with separators, etc.
The grammar will have someconflicts. Test it a lot in order todecide whether you can let jaccsolve them or whether you willhave to modify the grammar!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Storing the structure
While the parser recognizes aprogram, we cannot calculate a valuebut we can calculate a representationof the structure!
We forget about connectingterminals and about the derivationand concentrate in the othersyntactic categories that are used!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Storing the structure
While the parser recognizes aprogram, we cannot calculate a valuebut we can calculate a representationof the structure!
We forget about connectingterminals and about the derivationand concentrate in the othersyntactic categories that are used!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
What do we need?
Main class
We only need the name and the statement!
Class Declaration
We only need the name, the list of variable declarations and thelist of method declarations.
Method declaration
We only need the result type, the name, the list of parameters, thelist of variable declarations, the statement list and the returnedexpression.
For a statement, it depends . . .
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
What do we need?
Main class
We only need the name and the statement!
Class Declaration
We only need the name, the list of variable declarations and thelist of method declarations.
Method declaration
We only need the result type, the name, the list of parameters, thelist of variable declarations, the statement list and the returnedexpression.
For a statement, it depends . . .
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
What do we need?
Main class
We only need the name and the statement!
Class Declaration
We only need the name, the list of variable declarations and thelist of method declarations.
Method declaration
We only need the result type, the name, the list of parameters, thelist of variable declarations, the statement list and the returnedexpression.
For a statement, it depends . . .
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
What do we need?
Main class
We only need the name and the statement!
Class Declaration
We only need the name, the list of variable declarations and thelist of method declarations.
Method declaration
We only need the result type, the name, the list of parameters, thelist of variable declarations, the statement list and the returnedexpression.
For a statement, it depends . . .
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
What do we need?
Main class
We only need the name and the statement!
Class Declaration
We only need the name, the list of variable declarations and thelist of method declarations.
Method declaration
We only need the result type, the name, the list of parameters, thelist of variable declarations, the statement list and the returnedexpression.
For a statement, it depends . . .
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Assign
We need the identifier and theexpression.
Array element assign
We need the identifier, theindexing expression and the righthand side expression.
If
We need the expression and twostatements!
While
We need and expression and astatement!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Assign
We need the identifier and theexpression.
Array element assign
We need the identifier, theindexing expression and the righthand side expression.
If
We need the expression and twostatements!
While
We need and expression and astatement!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Assign
We need the identifier and theexpression.
Array element assign
We need the identifier, theindexing expression and the righthand side expression.
If
We need the expression and twostatements!
While
We need and expression and astatement!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Assign
We need the identifier and theexpression.
Array element assign
We need the identifier, theindexing expression and the righthand side expression.
If
We need the expression and twostatements!
While
We need and expression and astatement!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Assign
We need the identifier and theexpression.
Array element assign
We need the identifier, theindexing expression and the righthand side expression.
If
We need the expression and twostatements!
While
We need and expression and astatement!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Trees
All this looks very much like trees with diferent type and numberof children for different types of nodes!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Abstract syntax in Java
We need a datastructure inJava for the abstract syntaxtrees.
Problem
There will be a class for eachtype of node. But we wantthat all types of statementnodes can be used where astatement is needed! (and thesame for expressions by theway.)
Containers and components
Bricks are used as components,contained in a construction.But the construction can beused as a component in alarger construction!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Abstract syntax in Java
We need a datastructure inJava for the abstract syntaxtrees.
Problem
There will be a class for eachtype of node. But we wantthat all types of statementnodes can be used where astatement is needed! (and thesame for expressions by theway.)
Containers and components
Bricks are used as components,contained in a construction.But the construction can beused as a component in alarger construction!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Abstract syntax in Java
We need a datastructure inJava for the abstract syntaxtrees.
Problem
There will be a class for eachtype of node. But we wantthat all types of statementnodes can be used where astatement is needed! (and thesame for expressions by theway.)
Containers and components
Bricks are used as components,contained in a construction.But the construction can beused as a component in alarger construction!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Node for IF
public class If {public Exp e;public Statement s1,s2;
public If(Exp ae, Statement as1, Statement as2) {e=ae; s1=as1; s2=as2;
}}
How do we see that this builds a statement, that can be usedwhere a statement is required?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Node for IF
public class If {public Exp e;public Statement s1,s2;
public If(Exp ae, Statement as1, Statement as2) {e=ae; s1=as1; s2=as2;
}}
How do we see that this builds a statement, that can be usedwhere a statement is required?
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Node for IF
public class If extends Statement{public Exp e;public Statement s1,s2;
public If(Exp ae, Statement as1, Statement as2) {e=ae; s1=as1; s2=as2;
}}
Statements
public abstract class Statement {}
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Statements
Node for IF
public class If extends Statement{public Exp e;public Statement s1,s2;
public If(Exp ae, Statement as1, Statement as2) {e=ae; s1=as1; s2=as2;
}}
Statements
public abstract class Statement {}
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Building syntax trees
As the parser recognizes sentences of the different forms, it willbuild the abstract syntax trees.
Example
statement : IF ’(’ exp ’)’ statement ELSE statement{ $$ = new If($3, $5, $7); }
There is an abstract class for thesyntactic category statement andone class inheriting from it foreach production for statement!
And the same idea is used forexpressions!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Building syntax trees
As the parser recognizes sentences of the different forms, it willbuild the abstract syntax trees.
Example
statement : IF ’(’ exp ’)’ statement ELSE statement{ $$ = new If($3, $5, $7); }
There is an abstract class for thesyntactic category statement andone class inheriting from it foreach production for statement!
And the same idea is used forexpressions!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Building syntax trees
As the parser recognizes sentences of the different forms, it willbuild the abstract syntax trees.
Example
statement : IF ’(’ exp ’)’ statement ELSE statement{ $$ = new If($3, $5, $7); }
There is an abstract class for thesyntactic category statement andone class inheriting from it foreach production for statement!
And the same idea is used forexpressions!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Building syntax trees
As the parser recognizes sentences of the different forms, it willbuild the abstract syntax trees.
Example
statement : IF ’(’ exp ’)’ statement ELSE statement{ $$ = new If($3, $5, $7); }
There is an abstract class for thesyntactic category statement andone class inheriting from it foreach production for statement!
And the same idea is used forexpressions!
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Parsing minijava
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The project in the coursecontinues by using jacc to builda parser for minijava that, in theprocess of parsing a minijavaprogram, creates an abstractsyntax tree for the program!
You get the datastructure for theabstract syntax! (many classes,described in chapter 4 of thecourse book).
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Parsing minijava
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The project in the coursecontinues by using jacc to builda parser for minijava that, in theprocess of parsing a minijavaprogram, creates an abstractsyntax tree for the program!
You get the datastructure for theabstract syntax! (many classes,described in chapter 4 of thecourse book).
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern
Parsing minijava
Scanner Parser Types Trans.sourcecode
tokens AS AS IR
errors
The project in the coursecontinues by using jacc to builda parser for minijava that, in theprocess of parsing a minijavaprogram, creates an abstractsyntax tree for the program!
You get the datastructure for theabstract syntax! (many classes,described in chapter 4 of thecourse book).