📄 gnuctreeparser.g
字号:
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copyright (c) Non, Inc. 1998 -- All Rights Reserved
PROJECT: C Compiler
MODULE: GnuCTreeParser
FILE: GnuCTreeParser.g
AUTHOR: Monty Zukowski (jamz@cdsnet.net) April 28, 1998
DESCRIPTION:
This tree grammar is for a Gnu C AST. No actions in it,
subclass to do something useful.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
header {
package isis.anp.gnuc;
}
{
import isis.anp.common.TNode;
import antlr.MismatchedTokenException;
import antlr.NoViableAltException;
import antlr.RecognitionException;
import antlr.collections.AST;
import antlr.collections.impl.BitSet;
}
class GnuCTreeParser extends TreeParser;
options
{
importVocab = GnuC;
buildAST = false;
ASTLabelType = "TNode";
// Copied following options from java grammar.
codeGenMakeSwitchThreshold = 2;
codeGenBitsetTestThreshold = 3;
}
{
int traceDepth = 0;
public void reportError(RecognitionException ex) {
if ( ex != null) {
System.err.println("ANTLR Tree Parsing RecognitionException Error: " + ex.getClass().getName() + " " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(NoViableAltException ex) {
System.err.println("ANTLR Tree Parsing NoViableAltException Error: " + ex.toString());
TNode.printTree( ex.node );
ex.printStackTrace(System.err);
}
public void reportError(MismatchedTokenException ex) {
if ( ex != null) {
TNode.printTree( ex.node );
System.err.println("ANTLR Tree Parsing MismatchedTokenException Error: " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(String s) {
System.err.println("ANTLR Error from String: " + s);
}
public void reportWarning(String s) {
System.err.println("ANTLR Warning from String: " + s);
}
protected void match(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("match("+ttype+"); cursor is "+t);
super.match(t, ttype);
}
public void match(AST t, BitSet b) throws MismatchedTokenException {
//System.out.println("match("+b+"); cursor is "+t);
super.match(t, b);
}
protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("matchNot("+ttype+"); cursor is "+t);
super.matchNot(t, ttype);
}
public void traceIn(String rname, AST t) {
traceDepth += 1;
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceIn(rname, t);
}
public void traceOut(String rname, AST t) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceOut(rname, t);
traceDepth -= 1;
}
}
translationUnit options {
defaultErrorHandler=false;
}
: ( externalList )?
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
externalList
: ( externalDef )+
;
externalDef
: declaration
| functionDef
| asm_expr
| SEMI
| typelessDeclaration
;
typelessDeclaration
: #(NTypeMissing initDeclList SEMI)
;
asm_expr
: #( "asm" ( "volatile" )? LCURLY expr RCURLY ( SEMI )+ )
;
declaration
: #( NDeclaration
declSpecifiers
(
initDeclList
)?
( SEMI )+
)
;
declSpecifiers
: ( storageClassSpecifier
| typeQualifier
| typeSpecifier
)+
;
storageClassSpecifier
: "auto"
| "register"
| "typedef"
| functionStorageClassSpecifier
;
functionStorageClassSpecifier
: "extern"
| "static"
| "inline"
;
typeQualifier
: "const"
| "volatile"
;
typeSpecifier
: "void"
| "char"
| "short"
| "int"
| "long"
| "float"
| "double"
| "signed"
| "unsigned"
| structSpecifier ( attributeDecl )*
| unionSpecifier ( attributeDecl )*
| enumSpecifier
| typedefName
| #("typeof" LPAREN
( (typeName )=> typeName
| expr
)
RPAREN
)
| "__complex"
;
typedefName
: #(NTypedefName ID)
;
structSpecifier
: #( "struct" structOrUnionBody )
;
unionSpecifier
: #( "union" structOrUnionBody )
;
structOrUnionBody
: ( (ID LCURLY) => ID LCURLY
( structDeclarationList )?
RCURLY
| LCURLY
( structDeclarationList )?
RCURLY
| ID
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclarationList
: ( structDeclaration )+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclaration
: specifierQualifierList structDeclaratorList
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
specifierQualifierList
: (
typeSpecifier
| typeQualifier
)+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclaratorList
: ( structDeclarator )+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclarator
:
#( NStructDeclarator
( declarator )?
( COLON expr )?
( attributeDecl )*
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
enumSpecifier
: #( "enum"
( ID )?
( LCURLY enumList RCURLY )?
)
;
enumList
: ( enumerator )+
;
enumerator
: ID ( ASSIGN expr )?
;
attributeDecl:
#( "__attribute" (.)* )
| #( NAsmAttribute LPAREN expr RPAREN )
;
initDeclList
: ( initDecl )+
;
initDecl
{ String declName = ""; }
: #( NInitDecl
declarator
( attributeDecl )*
( ASSIGN initializer
| COLON expr
)?
)
;
pointerGroup
: #( NPointerGroup ( STAR ( typeQualifier )* )+ )
;
idList
: ID ( COMMA ID )*
;
initializer
: #( NInitializer (initializerElementLabel)? expr )
| lcurlyInitializer
;
initializerElementLabel
: #( NInitializerElementLabel
(
( LBRACKET expr RBRACKET (ASSIGN)? )
| ID COLON
| DOT ID ASSIGN
)
)
;
lcurlyInitializer
: #( NLcurlyInitializer
initializerList
RCURLY
)
;
initializerList
: ( initializer )*
;
declarator
: #( NDeclarator
( pointerGroup )?
( id:ID
| LPAREN declarator RPAREN
)
( #( NParameterTypeList
(
parameterTypeList
| (idList)?
)
RPAREN
)
| LBRACKET ( expr )? RBRACKET
)*
)
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -