370 likes | 821 Views
Language Processors. Language processor doesn’t mean “Compiler”. There are many other ways to use them. Different kinds of Language Processors. Lexical Syntactical Contextual. Lexical Processors. They are interested only in word There’s no interest in phrase structure.
E N D
Language processor doesn’t mean “Compiler” • There are many other ways to use them
Different kinds of Language Processors • Lexical • Syntactical • Contextual
Lexical Processors • They are interested only in word • There’s no interest in phrase structure
What is good for a Lexer void class ; “hello” pippo pluto static int double try while ‘c’ abstract (()( [] - = long goto for do
With a lexer you can build.... A Text Component with Syntax Highlight
Syntax Processors • They are interested in phrase structure • They doesn’t care about consistency
What is good for a Syntactical Processor public class a { private int a; public void a() { String a; a.method(); } }
Syntax processor in Ginipad pretty printer public class a { private int i; public void a() { String s; s = “Hello World”; String s1 = s.substring(7); } } public class a { private int i; public void a() {String s; s = “Hello World”; String s1 = s. substring(7);}}
Contextual Analizer • It validate type and scope rules
Contextual processors issues • Is it correct to use the identifier “a” both for class and method? • what is the type of identifier “s” inside the method? • Does the related type define a mehod call like “substring(int)”? public class a { private int s; public void a() { String s; s = “Hello World”; String s1 = s.substring(7); } }
A Case Study on Syntax Language Processors Building a Visual Tree Representation From the Source Code
Tree Component update Source Code Parser - AST Generator DST Traversal Abstract Syntax Tree Domain Specific Tree Representation AST Traversal Strategy
Grammar Design • We can start with a generic java grammar • We modify it to skip method bodies • Finally we use a parser generator tool to build the actual parser - AST generator
A simple Java Grammarfor Javacc: Class Node void Class() : {} { ( modifier() )* "class" <IDENTIFIER> ClassBody() } void modifier() : {} { "abstract" | "final” | "public” | "protected” | "private” | "static" }
Class Body void ClassBody() : {} { "{" ( Method() | Field() )* "}" }
Field void Field() : {} { ( modifier() )* Type() <IDENTIFIER> ";" } void PrimitiveType() : {} { "boolean” | "char” | "byte” | "short” | "int” | "long” | "float” | "double" } void Name() : {} { <IDENTIFIER> ( "." <IDENTIFIER>)* } void Type() : {} { ( PrimitiveType() | Name() ) ( "[" "]" )* }
Method void Method() : {} { ( modifier() )* ResultType() <IDENTIFIER> Parameters() ( "[" "]" )* ( Block() | ";" ) } void ResultType() : {} { "void” | Type() } void Parameters() : {} { "(" [ Parameter() ( "," Parameter() )* ] ")" } void Parameter() : {} { Type() <IDENTIFIER> }
Block JAVACODE void skipMethodBody() { Token tok; int nesting = 1; while (true) { tok = getToken(1); if (tok.kind == LBRACE) nesting++; if (tok.kind == RBRACE) { nesting--; if (nesting == 0) break; } tok = getNextToken(); } } void Block() : {} { "{" skipMethodBody() "}" }
A Sample Source Code public class MyClass { private int i; public int m1() { } }