Core Function Parser Engine

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> [Rules/Terminals] : TokenName Class name { Function __Construct ( <Parameters> ... ) { statements ... } } </pre> === Description === Parse text and have classes cr...")
 
m (1 revision)
 
(2 intermediate revisions by one user not shown)
Line 20: Line 20:
 
<pre>
 
<pre>
 
Terminal("Name")
 
Terminal("Name")
<pre>
+
</pre>
  
 
Defines this class as the object to be created when this Terminal is found in the source code
 
Defines this class as the object to be created when this Terminal is found in the source code
Line 29: Line 29:
 
<pre>
 
<pre>
 
Rule("<rule> ::= <bla bla bla>")
 
Rule("<rule> ::= <bla bla bla>")
<pre>
+
</pre>
  
 
Defines this class as the object to be created when this Rule is found in the source code
 
Defines this class as the object to be created when this Rule is found in the source code

Latest revision as of 12:37, 14 June 2015

[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