Core Function Parser Engine

From Sputnik Wiki
Jump to: navigation, search
[Rules/Terminals] : TokenName
Class name
{
	Function __Construct ( <Parameters> ... )
	{
		statements
		...
	}
}

Contents

Description

Parse text and have classes created and returned to you.

Terminal

Entered like:

Terminal("Name")

Defines this class as the object to be created when this Terminal is found in the source code

Rule

Entered like:

Rule("<rule> ::= <bla bla bla>")

Defines this class as the object to be created when this Rule is found in the source code

Class

The class object to be created

Function

Optional; If the __Construct is made then the Tokens will be linked to that class otherwise they may be null etc.

Remarks

This makes use of the GoldParser.

Its purpose is to provide a way to parse more complex files than simply regex alone can allow.

To make use of this you must first created a grammar file (.CGT) in GoldParser builder then create all classes for use (Simply opening parser without the rules/terminals will tell you what it requires to work) then use the Parser() function to spawn a new parser and Parse() to read text and parse it.

See example below

Example

[
Terminal("(EOF)")
Terminal("(Error)")
Terminal("(Whitespace)")
Terminal("(")
Terminal(")")
] : SpecialToken
Class myUselessTerminals{};
 
[Terminal("Float")] : SpecialToken
Class myFloat
{
	my $value;
	Function __Construct( $Value )
	{
		$this->$value = (Double)ObjToVar($Value);
	}
};
 
[Terminal("Integer")] : SpecialToken
Class myInteger
{
	my $value;
	Function __Construct( $Value )
	{
		$this->$value = (Double)ObjToVar($Value);
	}
};
 
[Rule("<Negate Exp> ::= ~'-' <Value>")] : SpecialToken
Class Negate
{
	my $value;
	Function __Construct( $value )
	{
		$this->$value = -(TokenClass($value)->$value);
	}
};
 
[Terminal("+")] : SpecialToken
Class opPlus
{
	my $value;
	Function __Construct( ) { }
	Function Calculate(&$Left, $Right)
	{
		return TokenClass($Left)->$value += TokenClass($Right)->$value;
	}
};
 
[Terminal("-")] : SpecialToken
Class opMinus
{
	my $value;
	Function __Construct( ) { }
	Function Calculate(&$Left, $Right)
	{
		return TokenClass($Left)->$value -= TokenClass($Right)->$value;
	}
};
 
[Terminal("*")] : SpecialToken
Class opMult
{
	my $value;
	Function __Construct( ) { }
	Function Calculate(&$Left, $Right)
	{
		return TokenClass($Left)->$value *= TokenClass($Right)->$value;
	}
};
 
[Terminal("/")] : SpecialToken
Class opDivide
{
	my $value;
	Function __Construct( ) { }
	Function Calculate(&$Left, $Right)
	{
		return TokenClass($Left)->$value /= TokenClass($Right)->$value;
	}
};
 
[
Rule("<Expression> ::= <Expression> '+' <Mult Exp>")
Rule("<Expression> ::= <Expression> '-' <Mult Exp>")
Rule("<Mult Exp> ::= <Mult Exp> '/' <Negate Exp>")
Rule("<Mult Exp> ::= <Mult Exp> '*' <Negate Exp>")
] : SpecialToken
Class Operation
{
	my $value;
	Function __Construct( $Left, $OP, $Right )
	{
		$this->$value = TokenClass($OP)->Calculate(&$Left, $Right);
	}
};
my $Trims = array( "<Value> ::= '(' <Expression> ')'" => "<Expression>" );
my $Parser = Parser("Test.cgt", "SpecialToken", $Trims);
println("Enter expressions to be parsed.");
println("Example 100+(50/2)");
while(true)
{
	my $Input = input("> ");
	try
	{
		if(isEmpty($Input))
		{
			println("You must type an expression to parse");
			continue;
		}
		my $ReturnToken = Parse($Parser, $Input);
		if(isToken($ReturnToken))
		{
			println(TokenClass($ReturnToken)->$Value);
		}
	}
	catch (Exception $e)
	{
		println("Error: " . $e->getMessage());
	}
}

Here is the Grammar file to use with the above source code

"Name"     = 'Calculator Sample Grammar'
"Author"   = 'Arsène von Wyss'
"Version"  = '0.1'
"About"    = 'Sample grammar for simple calculation expressions'

"Start Symbol" = <Expression>

"Case Sensitive" = false

Integer = {Digit}+
Float = {Digit}*'.'{Digit}+([Ee][+-]?{Digit}+)?

<Expression>  ::= <Expression> '+' <Mult Exp> 
               |  <Expression> '-' <Mult Exp> 
               |  <Mult Exp> 

<Mult Exp>    ::= <Mult Exp> '*' <Negate Exp> 
               |  <Mult Exp> '/' <Negate Exp> 
               |  <Negate Exp> 

<Negate Exp>  ::= '-' <Value> 
               |  <Value> 

<Value>       ::= Integer
               |  Float
               |  '(' <Expression> ')'
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox