/** * SpecC grammar file for JavaCC 15/6/01 * Contributed by * Hanmei Chen & Rukhsana Alam (Yamacraw Research Assistants) * School of Computing and Software Engineering * Southern Polytechnic State University * 1100 South Marietta Parkway * Marietta, GA, 30060 * * E-Mail : {hchen4, ralam}@spsu.edu * * Since SpecC is a superset of C, we built this grammar file based on * the C grammar file contributed by Mr.Doug South(dsouth@squirrel.com.au). * * This parser assumes that all SpecC source code has been preprocessed : * all #includes have been processed and all macros have been expanded. * This can be accomplished by the following command: * "gcc -P -E > " * My experience was : you need to rename ".sc" file to ".c" file to * preprocess it. * * The reason we are building this parser is to convert SpecC source code to * UML model recorded in XMI file. Therefore, our further work is to insert * semantic action to the grammar file so that the parser can take SpecC source * code and generate XMI file as output. This is our ongoing effort. Let us * know if you are interested in our work. * * We will maintain this grammar file. We will appreciate any bug or error * report. * **/ PARSER_BEGIN(SpecCParser) import java.util.*; public class SpecCParser{ private static String version = "SpecC Parser Version 0.1Alpha:"; // Hastable for storing typedef types private static Hashtable types = new Hashtable(); // Stack for determining when the parser // is parsing a typdef definition. private static Stack typedefParsingStack = new Stack(); // Returns true if the given string is // a typedef type. private static boolean isType(String type){ if(types.get(type) != null){ return true; } return false; } // Add a typedef type to those already defined private static void addType(String type){ types.put(type, Boolean.TRUE); } // Prints out all the types used in parsing the c source private static void printTypes(){ Enumeration enum = types.keys(); while(enum.hasMoreElements()){ System.out.println(enum.nextElement()); } } // Run the parser public static void main ( String args [ ] ) { SpecCParser parser ; // Hack to include type "special types" types.put("__signed__", Boolean.TRUE); types.put("__const", Boolean.TRUE); types.put("__inline__", Boolean.TRUE); types.put("__signed", Boolean.TRUE); if(args.length == 0){ System.out.println( version + " Reading from standard input . . ."); parser = new SpecCParser(System.in); } else if(args.length == 1){ System.out.println( version + " Reading from file " + args[0] + " . . ." ); try { parser = new SpecCParser(new java.io.FileInputStream(args[0])); }catch(java.io.FileNotFoundException e){ System.out.println( version + " File " + args[0] + " not found."); return ; } } else { System.out.println( version + " Usage is one of:"); System.out.println(" java SpecCParser < inputfile"); System.out.println("OR"); System.out.println(" java SpecCParser inputfile"); return ; } try { parser.TranslationUnit(); System.out.println( version + " SpecC program parsed successfully."); }catch(ParseException e){ System.out.println( version + " Encountered errors during parse."); } } } PARSER_END(SpecCParser) SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")> | <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/"> } TOKEN : { (["l","L"])? | (["l","L"])? | (["l","L"])?> | <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])*> | <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+> | <#OCTAL_LITERAL: "0" (["0"-"7"])*> | )? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"]> | <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+> | | // SpecC special tokens | <#BINDIGIT: ["0"-"1"]> | <#BINARY: ()+> | ) ["b", "B"]> | (["u", "U"] ["b", "B"] | ["b", "B"] ["u", "U"]))> } TOKEN : { | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | // SpecC Special tokens | | | | | | | | | | | | | | | | | | | | | | | | | | | | | } TOKEN : { ( | )*> | <#LETTER: ["$","A"-"Z","_","a"-"z"]> | <#DIGIT: ["0"-"9"]> } void TranslationUnit() : {} { (ExternalDeclaration())+ {printTypes();} } void ExternalDeclaration() : {} { ( LOOKAHEAD( FunctionDefinition() ) FunctionDefinition() | LOOKAHEAD( Declaration() ) Declaration() | LOOKAHEAD( SpecCDefinition() ) SpecCDefinition() ) } /***************************** SpecC Grammar Starts Here *****************************/ void SpecCDefinition() : {} { ImportDefinition() | LOOKAHEAD(BehaviorDeclarationOrDefinition()) BehaviorDeclarationOrDefinition() | ChannelDeclarationOrDefinition() | InterfaceDeclarationOrDefinition() | NoteDefinition() } void ImportDefinition() : {} { ";" } void BehaviorDeclarationOrDefinition() : {} { BehaviorSpecifier() PortList() ImplementsInterface() [ "{" InternalDefinitionList() "}" ] ";" } void BehaviorSpecifier() : {} { BehaviorName() } void ChannelDeclarationOrDefinition() : {} { ChannelSpecifier() PortList() ImplementsInterface() [ "{" InternalDefinitionList() "}" ] ";" } void ChannelSpecifier() : {} { ChannelName() } void PortList() : {} { [ "(" [LOOKAHEAD(PortDeclaration()) PortDeclaration() ("," PortDeclaration())*] ")" ] } void PortDeclaration() : {} { PortDirection() ParameterDeclaration() | InterfaceParameter() } void PortDirection() : {} { | | [] } void InterfaceParameter() : {} { ()+ } void ImplementsInterface() : {} { [ InterfaceName() ("," InterfaceName())* ] } void InternalDefinitionList() : {} { [ (InternalDefinition())+ ] } void InternalDefinition() : {} { LOOKAHEAD(FunctionDefinition()) FunctionDefinition() | LOOKAHEAD(Instantiation()) Instantiation() | Declaration() | NoteDefinition() } void Instantiation() : {} { InstanceDeclaringList() ";" } void InstanceDeclaringList() : {} { InstanceDeclarator() ("," InstanceDeclarator())* } void InstanceDeclarator() : {} { PortMappingOpt() } void PortMappingOpt() : {} { [ "(" [ PortMappingList() ] ")" ] } void PortMappingList() : {} { PortMapping() ("," PortMapping())* } void PortMapping() : {} { BitSlice() ( "&" BitSlice())* } void BitSlice() : {} { [LOOKAHEAD(2) "[" ConstantExpression() [LOOKAHEAD(2) ":" ConstantExpression()] "]" ] } void InterfaceDeclarationOrDefinition() : {} { InterfaceSpecifier() [ "{" InternalDeclarationList() "}" ] ";" } void InterfaceSpecifier() : {} { InterfaceName() } void InternalDeclarationList() : {} { [ (InternalDeclaration())+ ] } void InternalDeclaration() : {} { LOOKAHEAD(Declaration()) Declaration() | NoteDefinition() } void NoteDefinition() : {} { ["." ] "=" Note() ";" } void Note() : {} { ConstantExpression() } void TypedefName() : {} { } void BehaviorName() : {} { } void ChannelName(): {} { } void InterfaceName() : {} { } void SpecCStatement() : {} { LOOKAHEAD(ConcurrentStatement()) ConcurrentStatement() | FsmStatement() | ExceptionStatement() | TimingStatement() | WaitStatement() | WaitForStatement() | NotifyStatement() | NoteDefinition() } void ConcurrentStatement() : {} { CompoundStatement() | CompoundStatement() } void FsmStatement() : {} { "{" [TransitionList()] "}" } void TransitionList() : {} { (Transition())+ } void Transition() : {} { ":" [ "{" [CondBranchList()] "}" | CondBranchList() ] } void CondBranchList() : {} { (CondBranch())+ } void CondBranch() : {} { "(" Expression() ")" ( | ) ";" | ";" | ";" } void ExceptionStatement() : {} { CompoundStatement() ExceptionList() } void ExceptionList() : {} { [ (Exception())+ ] } void Exception() : {} { ParentEventList() CompoundStatement() | ParentEventList() CompoundStatement() } void ParentEventList() : {} { ["("] EventList() [")"] } void EventList() : {} { Event() ("," Event())* } void Event() : {} { } void TimingStatement() : {} { /*Compound*/ Statement() "{" [ConstraintList()] "}" } void ConstraintList() : {} { (Constraint())+ } void Constraint() : {} { "(" ";" ";" TimeOpt() ";" TimeOpt() ")" ";" } void TimeOpt() : {} { [ Time() ] } void Time() : {} { ConstantExpression() } void WaitStatement() : {} { ParentEventList() ";" } void WaitForStatement() : {} { Time() ";" } void NotifyStatement() : {} { ParentEventList() ";" | ParentEventList() ";" } /***************************** SpecC Grammar Ends Here *****************************/ void FunctionDefinition() : {} { [LOOKAHEAD(DeclarationSpecifiers()) DeclarationSpecifiers()] Declarator() [ DeclarationList() ] CompoundStatement() } void Declaration() : {} { DeclarationSpecifiers() [ InitDeclaratorList() ] ";" } void DeclarationList() : {} { (LOOKAHEAD(Declaration()) Declaration())+ } void DeclarationSpecifiers() : {} { StorageClassSpecifier() [ LOOKAHEAD(DeclarationSpecifiers()) DeclarationSpecifiers() ] | TypeSpecifier() [ LOOKAHEAD(DeclarationSpecifiers()) DeclarationSpecifiers() ] | TypeQualifier() [ LOOKAHEAD(DeclarationSpecifiers()) DeclarationSpecifiers() ] } void StorageClassSpecifier() : {} { ( | | | | | { typedefParsingStack.push(Boolean.TRUE); } ) } void TypeSpecifier() : {} { ( | | | | | | | | | | | "[" ConstantExpression() ":" ConstantExpression() "]" // changed by rukh on 02/09/01 at 3:30 p.m | StructOrUnionSpecifier() | EnumSpecifier() | LOOKAHEAD( { isType(getToken(1).image) } )TypedefName()) } void TypeQualifier() : {} { ( | ) } void StructOrUnionSpecifier() : {} { { typedefParsingStack.push(Boolean.FALSE); } StructOrUnion() ( LOOKAHEAD(3) [ ] "{" StructDeclarationList() "}" | ) { typedefParsingStack.pop(); } } void StructOrUnion() : {} { ( | ) } void StructDeclarationList() : {} { (StructDeclaration())+ } void InitDeclaratorList() : {} { InitDeclarator() ("," InitDeclarator())* { // Finished with a typedefDeclaration?? if(!(typedefParsingStack.empty()) && ((Boolean)typedefParsingStack.peek()).booleanValue()) { typedefParsingStack.pop(); } } } void InitDeclarator() : {} { Declarator() [ "=" Initializer() ] } void StructDeclaration() : {} { SpecifierQualifierList() StructDeclaratorList() ";" } void SpecifierQualifierList() : {} { TypeSpecifier() [ LOOKAHEAD(SpecifierQualifierList()) SpecifierQualifierList() ] | TypeQualifier() [ LOOKAHEAD(SpecifierQualifierList()) SpecifierQualifierList() ] } void StructDeclaratorList() : {} { StructDeclarator() ( "," StructDeclarator() )* } void StructDeclarator() : {} { ( LOOKAHEAD(3) Declarator() | [ Declarator() ] ":" ConstantExpression() ) } void EnumSpecifier() : {} { ( LOOKAHEAD(3) [ ] "{" EnumeratorList() "}" | ) } void EnumeratorList() : {} { Enumerator() ("," Enumerator())* } void Enumerator() : {} { [ "=" ConstantExpression() ] } void Declarator() : {} { [ Pointer() ] DirectDeclarator() } void DirectDeclarator() : { Token t; } { ( t = { if(!(typedefParsingStack.empty()) && ((Boolean)typedefParsingStack.peek()).booleanValue()) { addType(t.image); } } | "(" Declarator() ")" ) ( "[" [ ConstantExpression() ] "]" | LOOKAHEAD(3) "(" ParameterTypeList() ")" | "(" [ IdentifierList() ] ")" )* } void Pointer() : {} { "*" [ TypeQualifierList() ] [ Pointer() ] } void TypeQualifierList() : {} { (TypeQualifier())+ } void ParameterTypeList() : {} { ParameterList() ["," "..." ] } void ParameterList() : {} { ParameterDeclaration() (LOOKAHEAD(2) "," ParameterDeclaration())* | InterfaceParameter() (LOOKAHEAD(2) "," InterfaceParameter())* } void ParameterDeclaration() : {} { DeclarationSpecifiers() ( LOOKAHEAD(Declarator()) Declarator() | [ AbstractDeclarator() ] ) } void IdentifierList() : {} { ("," )* } void Initializer() : {} { ( AssignmentExpression() | "{" InitializerList() [","] "}" ) } void InitializerList() : {} { Initializer() (LOOKAHEAD(2) "," Initializer())* } void TypeName() : {} { SpecifierQualifierList() [ AbstractDeclarator() ] } void AbstractDeclarator() : {} { ( LOOKAHEAD(3) Pointer() | [Pointer()] DirectAbstractDeclarator() ) } void DirectAbstractDeclarator() : {} { ( LOOKAHEAD(2) "(" AbstractDeclarator() ")" | "[" [ConstantExpression()] "]" | "(" [ParameterTypeList()] ")" )( "[" [ ConstantExpression() ] "]" | "(" [ ParameterTypeList() ] ")" )* } void Statement() : {} { ( LOOKAHEAD(2) LabeledStatement() | CompoundStatement() | ExpressionStatement() | SelectionStatement() | LOOKAHEAD(IterationStatement()) IterationStatement() | JumpStatement() | SpecCStatement()) } void LabeledStatement() : {} { ( ":" Statement() | ConstantExpression() ":" Statement() | ":" Statement() ) } void ExpressionStatement() : {} { [ Expression() ] ";" } void CompoundStatement() : {} { "{" [ LOOKAHEAD(DeclarationList()) DeclarationList() ] (Statement())* "}" } void SelectionStatement() : {} { ( "(" Expression() ")" Statement() [ LOOKAHEAD(2) Statement() ] | "(" Expression() ")" Statement() ) } void IterationStatement() : {} { ( "(" Expression() ")" Statement() | Statement() "(" Expression() ")" ";" | "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() ) } void JumpStatement() : {} { ( ";" | ";" | ";" | [ Expression() ] ";" ) } void Expression() : {} { AssignmentExpression() ( "," AssignmentExpression() )* } void AssignmentExpression() : {} { LOOKAHEAD(UnaryExpression() AssignmentOperator()) UnaryExpression() AssignmentOperator() AssignmentExpression() | LOOKAHEAD(3) ConditionalExpression() } void AssignmentOperator() : {} { ( "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" ) } void ConditionalExpression() : {} { LogicalORExpression() [ "?" Expression() ":" ConditionalExpression() ] } void ConstantExpression() : {} { ConditionalExpression() } void LogicalORExpression() : {} { LogicalANDExpression() [ "||" LogicalORExpression() ] } void LogicalANDExpression() : {} { InclusiveORExpression() [ "&&" LogicalANDExpression() ] } void InclusiveORExpression() : {} { ExclusiveORExpression() [ "|" InclusiveORExpression() ] } void ExclusiveORExpression() : {} { ANDExpression() [ "^" ExclusiveORExpression() ] } void ANDExpression() : {} { EqualityExpression() [ "&" ANDExpression() ] } void EqualityExpression() : {} { RelationalExpression() [ ( "==" | "!=" ) EqualityExpression() ] } void RelationalExpression() : {} { ShiftExpression() [ ( "<" | ">" | "<=" | ">=" ) RelationalExpression() ] } void ShiftExpression() : {} { AdditiveExpression() [ ( "<<" | ">>" ) ShiftExpression() ] } void AdditiveExpression() : {} { MultiplicativeExpression() [ ( "+" | "-" ) AdditiveExpression() ] } void MultiplicativeExpression() : {} { ConcatExpression() [ ( "*" | "/" | "%" ) MultiplicativeExpression() ] } void CastExpression() : {} { ( LOOKAHEAD("(" TypeName() ")" CastExpression() ) "(" TypeName() ")" CastExpression() | UnaryExpression() ) } void ConcatExpression() : {} { CastExpression() ( "@" CastExpression())* } void UnaryExpression() : {} { ( LOOKAHEAD(3) PostfixExpression() | "++" UnaryExpression() | "--" UnaryExpression() | UnaryOperator() CastExpression() | ( LOOKAHEAD(UnaryExpression() ) UnaryExpression() | "(" TypeName() ")" ) ) } void UnaryOperator() : {} { ( "&" | "*" | "+" | "-" | "~" | "!" ) } void PostfixExpression() : {} { PrimaryExpression() ( LOOKAHEAD(3) "[" Expression() "]" | "(" [ LOOKAHEAD(ArgumentExpressionList() ) ArgumentExpressionList() ] ")" | "." | "->" | "++" | "--" | "[" ConstantExpression() ":" ConstantExpression() "]" )* } void PrimaryExpression() : {} { ( | Constant() | "(" Expression() ")" | | ) } void ArgumentExpressionList() : {} { AssignmentExpression() ( "," AssignmentExpression() )* } void Constant() : {} { | | | | | | | }