Computer Languages - minijava - Concrete & Abstract...

95
Parsing Syntactic categories Concrete syntax Abstract syntax Composite pattern Computer Languages minijava - Concrete & Abstract syntax 1 Parsing 2 Syntactic categories 3 Concrete syntax 4 Abstract syntax 5 Composite pattern February 4

Transcript of Computer Languages - minijava - Concrete & Abstract...

Page 1: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 2: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 3: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 4: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 5: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 6: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 7: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 8: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 9: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 10: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 11: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 12: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 13: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 14: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 15: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 16: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 17: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 18: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 19: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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)

Page 20: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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)

Page 21: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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)

Page 22: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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)

Page 23: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 24: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 25: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 26: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 27: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 28: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 29: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 30: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 31: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 32: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 33: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 34: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 35: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 36: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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.

Page 37: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 38: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 39: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 40: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 41: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 42: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 43: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 44: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 45: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 46: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 47: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 48: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 49: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 50: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 51: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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

Page 52: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’}’

Page 53: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’}’

Page 54: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’}’

Page 55: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’}’

Page 56: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 57: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 58: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 59: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 60: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 61: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 62: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 63: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 ’;’

Page 64: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 65: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 66: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 67: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 68: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 69: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 70: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 71: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 . . .

Page 72: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 . . .

Page 73: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 . . .

Page 74: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 . . .

Page 75: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 . . .

Page 76: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 77: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 78: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 79: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 80: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 81: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 82: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 83: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 84: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 85: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 86: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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?

Page 87: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 {}

Page 88: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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 {}

Page 89: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 90: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 91: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 92: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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!

Page 93: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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).

Page 94: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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).

Page 95: Computer Languages - minijava - Concrete & Abstract syntaxlacl.u-pec.fr/gava/cours/MIAGE/ParsingJavaL2/cours/lecture5.pdf · In Java and minijava: a class with a static main method!

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).