coins.cfront
Class Parser

java.lang.Object
  extended bycoins.cfront.Parser
All Implemented Interfaces:
TokenId, TypeId

public class Parser
extends java.lang.Object
implements TokenId, TypeId

Parser for the C language.


Field Summary
static java.lang.String cppCommand
          The command string for invoking a preprocessor.
(package private)  Debug debug
           
(package private)  Evaluator evaluator
           
 int fDbgLevel
           
static java.lang.String invalidCChar
          Used to generate an identifier that never appears in a source program.
(package private)  Lex lex
           
(package private)  java.util.LinkedList pragmaList
           
 
Fields inherited from interface coins.ast.TokenId
AND_E, ANDAND, ARROW, ASM, AUTO, BAD_TOKEN, BREAK, CASE, CAST_OP, CHAR, CHAR_CONST, COND_OP, CONST, CONTINUE, DEFAULT, DIV_E, DO, DOUBLE, DOUBLE_CONST, ELLIPSIS, ELSE, ENUM, EOF, EQ, EXOR_E, EXTERN, FLOAT, FLOAT_CONST, FOR, FUNCALL, GE, GOTO, IDENTIFIER, IF, IGNORE, INDEX_OP, INLINE, INT, INT_CONST, LE, LONG, LONG_CONST, LONG_DOUBLE_CONST, LONGLONG_CONST, LSHIFT, LSHIFT_E, MINUS_E, MINUSMINUS, MOD_E, MUL_E, MUTABLE, NEQ, OR_E, OROR, PLUS_E, PLUSPLUS, PRAGMA, REGISTER, RESTRICT, RETURN, RSHIFT, RSHIFT_E, SHORT, SIGNED, SIZEOF, SKIP_GCC_ASM, SKIP_GCC_ATTRIBUTE, STATIC, STRING_L, STRING_WL, STRUCT, SWITCH, TYPEDEF, TYPEDEF_NAME, UINT_CONST, ULONG_CONST, ULONGLONG_CONST, UNION, UNSIGNED, VOID, VOLATILE, WHILE
 
Fields inherited from interface coins.ast.TypeId
ARRAY_T, CHAR_T, CONST_T, DOUBLE_T, ELLIPSIS_T, ENUM_BEGIN, ENUM_END, FLOAT_T, FUNCTION_T, INT_T, LONG_DOUBLE_T, LONG_LONG_T, LONG_T, NO_DIMENSION_T, OFFSET_T, POINTER_T, RESTRICT_T, RETURN_T, S_AUTO, S_EXTERN, S_INLINE, S_NONE, S_REGISTER, S_STATIC, SHORT_T, SIGNED_T, SIZE_T, STRUCT_BEGIN, STRUCT_END, UNION_BEGIN, UNION_END, UNSIGNED_T, VOID_T, VOLATILE_T
 
Constructor Summary
Parser(IoRoot ioroot, Lex lex, ToHirC tohirc)
          Constructs a parser.
 
Method Summary
protected  java.lang.String decodeTagName(java.lang.String tagName)
           
protected  void enterNewEnvironment()
           
protected  void exitEnvironment()
           
 SymbolTable getCurrentSymbolTable()
           
protected  java.lang.String getNewEncodedTag(java.lang.String name)
           
 boolean hasNext()
          Returns false if the parser reaches the end of file.
(package private)  boolean isTypedefedType(java.lang.String typename)
           
protected  Aggregate lookupEncodedTag(java.lang.String tagName)
           
protected  Declarator makeDeclarator(java.lang.String name, java.lang.String fname, int line)
          Creates a Declarator object.
protected  Function makeFunction(Declarator decl, Stmnt body)
          Creates a Function object.
protected  Struct makeStruct(java.lang.String name, DeclaratorList mems, java.lang.String fname, int line)
          Creates a Struct object.
protected  Union makeUnion(java.lang.String name, DeclaratorList mems, java.lang.String fname, int line)
          Creates a Union object.
 void parse(Backend backend, boolean verbose)
          Parses the whole source file.
 ASTList read()
          Reads a top-level declaration and returns a parse tree.
 void recordSymbol(java.lang.String name, Declarator d)
           
protected  java.lang.String recordTag(java.lang.String tagName)
          Declares that a struct/union will be declared later in the current name scope.
protected  void recordTag(java.lang.String tagName, Aggregate a)
           
static java.io.InputStream runCpp(java.lang.String source)
          Runs a preprocessor (gcc -E) and returns the input stream obtaining the output of the preprocessor.
 void showEpilogue()
           
protected  void showErrorMessage(ParseError e)
           
 void showErrorMessage(Stmnt pos, java.lang.String message)
           
 void showWarningMessage(java.lang.String message)
           
 long sizeofStruct(java.lang.String tagName)
           
 long sizeofUnion(java.lang.String tagName)
           
protected  java.lang.String toEncodedTag(java.lang.String tagName)
          Returns a new unique tag name if the given tag name overrides another one declared in an outer scope.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

invalidCChar

public static final java.lang.String invalidCChar
Used to generate an identifier that never appears in a source program.

See Also:
Constant Field Values

debug

final Debug debug

lex

final Lex lex

evaluator

Evaluator evaluator

pragmaList

java.util.LinkedList pragmaList

fDbgLevel

public final int fDbgLevel

cppCommand

public static java.lang.String cppCommand
The command string for invoking a preprocessor.

Constructor Detail

Parser

public Parser(IoRoot ioroot,
              Lex lex,
              ToHirC tohirc)
Constructs a parser.

Parameters:
lex - a lexical analyzer
Method Detail

runCpp

public static java.io.InputStream runCpp(java.lang.String source)
                                  throws java.io.IOException
Runs a preprocessor (gcc -E) and returns the input stream obtaining the output of the preprocessor.

Throws:
java.io.IOException

isTypedefedType

boolean isTypedefedType(java.lang.String typename)

parse

public void parse(Backend backend,
                  boolean verbose)
           throws java.io.IOException
Parses the whole source file. This method repeatedly reads a top-level declaration and invokes Backend.compile(). See cfront.Backend#compile(ast.ASTList)

Parameters:
verbose - true if an epilogue message is printed out.
Throws:
java.io.IOException

hasNext

public boolean hasNext()
                throws java.io.IOException
Returns false if the parser reaches the end of file.

Throws:
java.io.IOException

makeDeclarator

protected Declarator makeDeclarator(java.lang.String name,
                                    java.lang.String fname,
                                    int line)
Creates a Declarator object. This method should be overwritten if the client provides a subclass of Declarator and the parser must instantiate that subclass. The implementation in this class is as follows:


makeFunction

protected Function makeFunction(Declarator decl,
                                Stmnt body)
Creates a Function object. This method should be overwritten if the client provides a subclass of Function and the parser must instantiate that subclass. The implementation in this class is as follows:


makeStruct

protected Struct makeStruct(java.lang.String name,
                            DeclaratorList mems,
                            java.lang.String fname,
                            int line)
Creates a Struct object. This method should be overwritten if the client provides a subclass of Struct and the parser must instantiate that subclass. The implementation in this class is as follows:


makeUnion

protected Union makeUnion(java.lang.String name,
                          DeclaratorList mems,
                          java.lang.String fname,
                          int line)
Creates a Union object. This method should be overwritten if the client provides a subclass of Union and the parser must instantiate that subclass. The implementation in this class is as follows:


showErrorMessage

protected void showErrorMessage(ParseError e)

showErrorMessage

public void showErrorMessage(Stmnt pos,
                             java.lang.String message)

showWarningMessage

public void showWarningMessage(java.lang.String message)

showEpilogue

public void showEpilogue()

recordSymbol

public void recordSymbol(java.lang.String name,
                         Declarator d)

enterNewEnvironment

protected void enterNewEnvironment()

exitEnvironment

protected void exitEnvironment()

lookupEncodedTag

protected Aggregate lookupEncodedTag(java.lang.String tagName)

toEncodedTag

protected java.lang.String toEncodedTag(java.lang.String tagName)
Returns a new unique tag name if the given tag name overrides another one declared in an outer scope.


decodeTagName

protected java.lang.String decodeTagName(java.lang.String tagName)

getNewEncodedTag

protected java.lang.String getNewEncodedTag(java.lang.String name)

recordTag

protected java.lang.String recordTag(java.lang.String tagName)
                              throws ParseError
Declares that a struct/union will be declared later in the current name scope.

Throws:
ParseError

recordTag

protected void recordTag(java.lang.String tagName,
                         Aggregate a)
                  throws ParseError
Throws:
ParseError

sizeofStruct

public long sizeofStruct(java.lang.String tagName)
                  throws ParseError
Throws:
ParseError

sizeofUnion

public long sizeofUnion(java.lang.String tagName)
                 throws ParseError
Throws:
ParseError

read

public ASTList read()
             throws java.io.IOException
Reads a top-level declaration and returns a parse tree. This method returns a LIST of parse tree because it may divide the given declaration into a few simple declaration although the number of elements of the returned list is usually one. For example, is divided into the following two simple declarations:

Note that this method may return null.

Throws:
java.io.IOException

getCurrentSymbolTable

public SymbolTable getCurrentSymbolTable()