Want to parse a string into java objects using some grammar like regular expression or binary operations(expressions).?????
Antlr is a good tool to use. It will generate java/c++ code easily which helps to create java objects
(http://antlr2.org/)
Below are the steps to define grammar & generate java code
Define grammar:
Tokenization(lexer):
Define tokens (eg. AND,OR, [a...b]*). This will be used to identify the tokens in the string given.
Parsing(Parser):
Define grammar/rules such that It can build the tree. Antlr will parse the given string as per the rules and build AST tree.
(eg. expr : expr2 (OR^ expr2)*; pexpr :(' expr ')' -> expr;)
TreeParser:
Define grammar/rules to specify action at each node. These action methods can be written in java
Now it will perform the actions specified like (construct a java object/calculate a value etc)
Generate java code :
Download the antlrworks (http://www.antlr2.org/download.html)
run the antlrworks (java -jar antlrworks-***.jar)
open the grammar files defined above and click generate
Links to sample grammar files
Lexar/Parser: https://docs.google.com/file/d/0Bzsr76b-5vDma1Jwa3dYZEZNXzg/edit
TreeParser : https://docs.google.com/file/d/0Bzsr76b-5vDmWGs4aXJoVlFqeWM/edit
This is an example of AND/OR binary tree which can be constructed from expression
PS: This is how compilers work
Antlr is a good tool to use. It will generate java/c++ code easily which helps to create java objects
(http://antlr2.org/)
Below are the steps to define grammar & generate java code
Define grammar:
Tokenization(lexer):
Define tokens (eg. AND,OR, [a...b]*). This will be used to identify the tokens in the string given.
Parsing(Parser):
Define grammar/rules such that It can build the tree. Antlr will parse the given string as per the rules and build AST tree.
(eg. expr : expr2 (OR^ expr2)*; pexpr :(' expr ')' -> expr;)
TreeParser:
Define grammar/rules to specify action at each node. These action methods can be written in java
Now it will perform the actions specified like (construct a java object/calculate a value etc)
Generate java code :
Download the antlrworks (http://www.antlr2.org/download.html)
run the antlrworks (java -jar antlrworks-***.jar)
open the grammar files defined above and click generate
Links to sample grammar files
Lexar/Parser: https://docs.google.com/file/d/0Bzsr76b-5vDma1Jwa3dYZEZNXzg/edit
TreeParser : https://docs.google.com/file/d/0Bzsr76b-5vDmWGs4aXJoVlFqeWM/edit
This is an example of AND/OR binary tree which can be constructed from expression
PS: This is how compilers work
No comments:
Post a Comment