dcg-based-parser

It is a simple programming language step-by-step implementation using DCG-parsing power. It also evaluates a code and optinally builds a parse tree. It is tested with gprolog (1.4.4) but it possibly can be used with other prolog translators. It can be used as an example of a simple DCG-parser development.

Step 1. It is a calculator for the real arithmetic. It works with any formula compliant with the next grammar in BNF:


Input  Expression | nothing
Expression  Term Expression_rest
Expression_rest  + Term Expression_rest | - Term Expression_rest | nothing
Term  Factor Term_Rest
Term_Rest  * Factor Term_Rest | /  Factor Term_Rest | nothing
Factor  Element ^ Factor | - Factor | + Factor | Element
Element  ( Expression ) | number

Use `hoc1.pro' for this step. Use `hoc1x.pro' if you want to have a parse tree. The tree is printed in text linear form by default. If a graphic tree is required use the aim hoc1g to produce a file in dot-format.

Step 2. It is missed. It supposes an addition of a support for one letter named variables.

Step 3. It is a calculator for the real arithmetic with constants (pi, e, phi), built-in functions (sin, ln, sqrt, arctg, log2, fib), and variables. It works with any formula compliant with the next grammar in BNF:


Input  Assignment | Expression | nothing
Assignment  identifier = Expression
Expression  Term Expression_rest | Assignment
Expression_rest  + Term Expression_rest | - Term Expression_rest | nothing
Term  Factor Term_rest
Term_rest  * Factor Term_rest | / Factor Term_rest | nothing
Factor  Element ^ Factor | - Factor | + Factor | Element
Element  ( Expression ) | built_in ( Expression ) | identifier | number

Use `hoc3.pro' for this step. Use `hoc3x.pro' if you want to have a parse tree. For a graphic tree use the aim hoc3g as usual. The file `hoc3.pl' is a wrapper to use it for swi-prolog – it gives the idea how to use such wrappers with other samples.

Step 4. It is for the same language but it generates a code and later executes it. It allows to add the support for the programming language statements later. This impementation contains an interpreter which executes the generated codes. The previous implementations just evaluate an inputted text immediately. Use `hoc4.pro' for this step.

Step 5. It adds C-like relational operators (<, >, ==, !==, <=, >=), C-like logical operators (||, &&, !), loop (while), conditional (if) and print statements. It also prints the stream of entered lexemes and the result of translation in RPN.


Input  Assignment | Formula | Statement | nothing
Statement  while Condition Statement
 | print Formula
 | if Condition Statement else Statement
 | if Condition Statement
 | { Statement_list }
 | Formula Statement_list Statement Statement_list | nothing Formula Relation Formula_rest | Assignment Formula_rest && Relation Formula_rest | || Relation Formula_rest | nothing Relation Expression == Expression | Expression != Expression | Expression <= Expression  | Expression >= Expression | Expression > Expression | Expression < Expression | Expression Assignment Lvalue = Formula Expression Term Expression_rest Expression_rest + Term Expression_rest | - Term Expression_rest | nothing Term Factor Term_Rest Term_Rest * Factor Term_Rest | / Factor Term_Rest | nothing Factor Element ^ Factor | - Factor | + Factor | Element Element Condition | ! Element | built_in ( Expression ) | identifier | number Condition ( Formula ) Lvalue identifier

Use `hoc5.pro' for this step. Use `hoc5x.pro' if you want to have a parse tree. For a graphic tree use the aim hoc5g.

Step 6. It adds support for string output and subroutines (procedures and functions).


Input  Assignment | Formula | Statement | Define_function | nothing
Define_function  func Lvalue () Statement | proc Lvalue () Statement
Statement  while Condition Statement
 | print Print_list
 | if Condition Statement else Statement
 | if Condition Statement
 | { Statement_list }
 | Procedure_call ( Argument_list )
 | return Formula
 | return
 | Formula
Argument_list  Formula , Argument_list | Formula | nothing
Statement_list  Statement Statement_list | nothing
Formula  Relation Formula_rest | Assignment
Formula_rest  && Relation Formula_rest | || Relation Formula_rest | nothing
Relation  Expression == Expression | Expression != Expression | Expression <= Expression 
 | Expression >= Expression | Expression > Expression | Expression < Expression | Expression
Assignment  Lvalue = Formula | Local_id = Formula
Expression  Term Expression_rest
Expression_rest  + Term Expression_rest | - Term Expression_rest | nothing
Term  Factor Term_Rest
Term_Rest  * Factor Term_Rest | /  Factor Term_Rest | nothing
Factor  Element ^ Factor | - Factor | + Factor | Element
Element  Condition
| ! Element
| built_in ( Expression )
| Function_call ( Argument_list )
| identifier
| Local_id
| number
Condition  ( Formula )
Print_list  Formula , Print_list | Formula | String , Print_list | String 
Local_id  $ number
String  " text "
Lvalue  identifier
Procedure_call  identifier
Function_call  identifier

Use `hoc6.pro' for this step. Use `hoc6x.pro' if you want to have a parse tree. For a graphic tree use the aim hoc6g. A file `hoc6.pl' provides a wrapper for swi-prolog system.

Step 7. It should provide new statements (do, for, break, continue, unless, ...), new operators (++, --, +=, /=, ...), local variables (static and automatic), arrays, constants with identifiers, ... It is still not realised. Don't hesitate to make it!

Other Project's Files

gtree.pro – it is used by the parser graphic tree drawing routine for `hoc1x.pro', `hoc3x.pro', and `hoc5x.pro'.

gtreex.pro – it is used by the parser graphic tree drawing routine for `hoc6x.pro'.

test5.in – a test for `hoc5.pro' and 'hoc5x.pro'.

test6.in – a test for `hoc6.pro' and 'hoc6x.pro'.

www/expr.html – a web interface page for an interactive parse tree generation.

www/expr.sh – a web interface script for an interactive parse tree generation.