Skip to content

Childcity/SimpleLanguageParser

Repository files navigation

SimpleLanguageParser

Parser for language, look like C/C++

Algorithm of this parser

Input Text       (written with my own syntax)

   |
  \|

Lexical Analyzer     (devide text on lexems and check all allowed symbols)

  |
 \|

List of Lexems      ( intermediate representation of input lexems as json list)

  |
 \|

Syntactic Analyzer    (check on valid syntax and build AST tree as result)

  |
 \| 

  AST Tree      ( intermediate representation of input program)\

  |
 \|

  Reverse
Polish Notation     (well, no comments)
and Executing

Grammar of My Language in EBNF format (GorodLang)

Gorod = Block .

Block = "{"       
            {VarDefinition}
            {Statement} 
        "}" .

VarDefinition = Type Ident {"," Ident} ";" .

Type = "int" .

Ident = letter {letter | digit | "_"} .

Statement = [Assignment] ";"
            | "read" "(" Ident ")" ";"
            | "write" "(" Expr ")" ";"
            | "if" "(" LogicExpr ")" Block 
            | "for" Assignment "to" Expr "by" Expr "while" "(" LogicExpr ")" Block "rof" ";" . 

Assignment = Ident "=" Expr .

Expr = CondExpr
       | Add . 

Add = Mult [( "+" | "-" ) Mult] .

Mult = Power [( "*" | "/" ) Power] .

Power = Group ["^" Power] .

Group = "(" Expr ")"  
        | Number
        | Ident .

CondExpr = LogicExpr ["?" Expr ":" Expr] .

LogicExpr = Ident ("<" | ">") Expr .

Number = [ "-" ] digit{digit} .

Examle of input program

{
  int k; int sum, input, i;
  sum = 0;
  for i=0; to 3^144 by k+1 while(sum<1000){
		    read(input);
		  if (input > 0)
		  {
		    sum = sum + input;
		    write(sum);
		  }
		
	}rof;
  sum = sum^2;
  write(sum);
}

Example of lexical analyzer result (json table of lexems)

[
    {
        "line": 1,
        "posInLine": 1,
        "type": "token",
        "value": "{"
    },
    {
        "line": 2,
        "posInLine": 3,
        "type": "token",
        "value": "int"
    },
    {
        "id": 0,
        "line": 2,
        "posInLine": 7,
        "type": "ident",
        "value": "k"
    },
    
    ........

more here

Example of AST tree after Syntactical anal

ASTree realization ast_tree

About

Parser for language, look like C/C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published