Skip to content

kupihleba/C-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# bottom-up parser

Libraries: Lexer

Example

DSL for lexer rules:

var PROGRAM, NAMESPACE, ... Rule
	
PROGRAM.Name = "PROGRAM" 
PROGRAM.ExprSet = []Expression{
		{ref(&USING)},
		{ref(&CLASS_DECL)},
		{ref(&METHOD)},
		{ref(&NAMESPACE)},
		{ref(&VAR_DECL)},
		{ref(&PROGRAM), ref(&USING)},
		{ref(&PROGRAM), ref(&CLASS_DECL)},
		{ref(&PROGRAM), ref(&METHOD)},
		{ref(&PROGRAM), ref(&NAMESPACE)},
		{ref(&PROGRAM), ref(&VAR_DECL)},
	}
  
  NAMESPACE.Name = "NAMESPACE"
	NAMESPACE.ExprSet = []Expression{
		{tok("namespace"), tok("IDENTIFIER"), tok("{"), tok("}")},
		{tok("namespace"), tok("IDENTIFIER"), tok("{"), ref(&PROGRAM), tok("}")},
	}
  ...
  
  Grammar = append(Grammar, PROGRAM, NAMESPACE...)

Input

using System;

/**
 * Documentation here
 */
class Test {
    string str; // Some comments
    int val;

    void test() {}

    static int calc();
}

Rules loaded:

PROGRAM(0): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 0
PROGRAM(1): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 1
PROGRAM(2): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 2
PROGRAM(3): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 3
PROGRAM(4): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 4
PROGRAM(5): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 5
PROGRAM(6): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 6
PROGRAM(7): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 7
PROGRAM(8): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 8
PROGRAM(9): [[USING KEYWORD] [CLASS DECLARATION] [METHOD DECLARATION] [NAMESPACE] [VARIABLE DECLARATION] [PROGRAM USING KEYWORD] [PROGRAM CLASS DECLARATION] [PROGRAM METHOD DECLARATION] [PROGRAM NAMESPACE] [PROGRAM VARIABLE DECLARATION]] -> 9
...
NAMESPACE(0): [[namespace IDENTIFIER { }] [namespace IDENTIFIER { PROGRAM }]] -> 47
NAMESPACE(1): [[namespace IDENTIFIER { }] [namespace IDENTIFIER { PROGRAM }]] -> 48
...

Result

Successfully parsed in 122,745,064 iterations

Tree: [- - - 16 0 - - - - 43 - 18 - 17 - 44 - 18 - 17 - 45 - - - - - 39 - 42 - 44 - - - - 35 13 14 15 15 - 10 6]

Written as a lab for BMSTU