Question on how to use AST
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   James_Mello
Posted On:   Monday, December 18, 2006 07:05 PM

I'm prototyping a simple syntax which looks similar to an SQL query.... For example select *:* from // To do the query parsing, I've written the following grammer : header { package com.apptechsys.grammer; } class MyParser extends Parser; options { buildAST = true; k = 2; } tokens { SELECT_STATEMENT; FILTER_CLAUSE; FROM_CLAUSE; PATH_CLAUSE; FROM; FILTER; PATH; } statement : selectStatement ; selectStatement : "select" filterClause fromClause SEMI! { #selectStat   More>>

I'm prototyping a simple syntax which looks similar to an SQL query....

For example

select *:* from //


To do the query parsing, I've written the following grammer :

			


header
{
package com.apptechsys.grammer;
}

class MyParser extends Parser;

options
{
buildAST = true;
k = 2;
}

tokens
{
SELECT_STATEMENT;
FILTER_CLAUSE;
FROM_CLAUSE;
PATH_CLAUSE;
FROM;
FILTER;
PATH;
}

statement
: selectStatement
;

selectStatement :
"select" filterClause fromClause SEMI!
{ #selectStatement = #([SELECT_STATEMENT, "selectStatement"], #selectStatement); }
;

fromClause
: "from" pathClause
{ #fromClause = #([FROM_CLAUSE, "fromClause"], #fromClause); }
;

pathClause
: SLASH (SLASH)? filterClause
;

filterClause
: (STAR|ID)COLON(STAR|ID)
{ #filterClause = #([FILTER_CLAUSE, "filterClause"], #filterClause); }
;


class MyLexer extends Lexer;

options
{
k=2;
charVocabulary = 'u0000'..'u007F';
caseSensitive = false;
}

ID options { testLiterals = true; } : ('a'..'z'|'0'..'9')+ ;

SLASH: '/';

COLON: ':';

STAR: '*';

SEMI: ';' ;

WS :
(' '
| ' '
| '
' '
' { newline(); }
| '
' { newline(); }
)
{ $setType(Token.SKIP); }
;

class MyTreeParser extends TreeParser;

options
{
buildAST = true;
}

expr returns [ SelectObject selectObject = null; ]
{
}
: selectObject = selectExpr
;

selectExpr
returns [ SelectObject selectObject = null; ]
:
{ String string = null; }
#(SELECT_STATEMENT "select" string = filterExpr )
{
selectObject = new SelectObject();
selectObject.setFilter(string);
}
;

filterExpr returns [ String s = null ]:
#(FILTER_CLAUSE firstPart:starOrID secondPart:COLON thirdPart:starOrID )
{ s = firstPart.getText() + secondPart.getText() + thirdPart.getText(); }
;

starOrID :
(STAR|ID)
;


I've got the AST, but now it's a matter of translating the tree contents from the AST to useful method calls. I'm not quite sure how to use the AST's "properly" and was wondering if someone could provide a bit of guideance. For instance, for a complex set of AST tree parsers, do I write a specific object representation for all the data I want, and then a specific rule for the AST? This seems a bit wasteful and forces me to write a lot of AST productions to get to the data. Furthermore it seems to me that the top level production would be very messy as it would require a ton of checks for the type... Because of this, I feel like I'm missing something rather fundemental.

-- Cheers
-- James    <<Less
About | Sitemap | Contact