edu.nyu.cascade.c
Class CParserState

java.lang.Object
  extended by edu.nyu.cascade.c.CParserState
All Implemented Interfaces:
State

public class CParserState
extends Object
implements State

The global state for parsing C. This class provides a simplified symbol table that is organized as a stack of parsing contexts, with a new context being pushed onto the stack through start() and popped again through commit() or abort().

Version:
$Revision: 1.15 $
Author:
Robert Grimm

Nested Class Summary
protected static class CParserState.Context
          A parsing context.
 
Field Summary
protected  Annotation annotation
          The current annotation, if any.
protected static boolean DEBUG
          The flag for whether to print debug information to the console.
protected static int FLAG_MODIFIED
          The flag for having modified the bindings.
protected static int FLAG_PARAMS
          The flag for having parsed a function parameter list.
protected static int FLAG_SCOPE
          The flag for scopes.
protected static int FLAG_STRUCTURE
          The flag for structure/union declaration lists.
protected static int FLAG_TYPE_SPEC
          The flag for having parsed a type specifier.
protected static int FLAG_TYPEDEF
          The flag for typedefs.
protected  int nesting
          The current nesting level.
protected static int POOL_INCR
          The increment of the context pool.
protected static int POOL_INIT
          The initial size of the context pool.
protected  CParserState.Context top
          The top of the context stack.
 
Constructor Summary
CParserState()
          Create a C parser state object.
 
Method Summary
 void abort()
           
protected  void addToPool(CParserState.Context c)
          Return the specified context to the pool, clearing it along the way.
 Node annotate(Node node)
          Annotate the specified node.
 void bind(String id)
          Implicitly bind the specified identifier.
 void bind(String id, boolean isType)
          Explicitly bind the specified identifier.
 void commit()
           
 void enterStructure()
          Enter a structure declaration list.
 void exitStructure()
          Exit a structure declaration list.
protected  void fillPool(int n)
          Add the specified number of fresh contexts to the pool.
 void functionDeclarator()
          Record a function declarator.
 void ident(String ident, Location location)
          Record an ident directive.
 boolean isType(String id)
          Determine whether the specified identifier names a type.
 boolean isValid(Node idl)
          Determine whether a declaration actually is a declaration.
 void lineMarker(String file, int line, String isStartFile, String isReturnToFile, String isSystemHeader, String isExternC, Location location)
          Record a line marker.
 void mark()
          Mark the current annotation.
 void parameters()
          Record a function parameter list.
protected  CParserState.Context pop()
          Pop a context from the context stack.
 void popScope()
          Exit the last scope.
 void pragma(String directive, Location location)
          Record a pragma.
protected  void push(CParserState.Context c)
          Push the specified context onto the context stack.
 void pushScope()
          Enter a new scope.
 void reset(String file)
           
 void start()
           
protected  CParserState.Context takeFromPool()
          Take a context from the pool, refilling the pool if necessary.
 void typedef()
          Record a typedef storage class specifier.
 void typeSpecifier()
          Record a type specifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

protected static final boolean DEBUG
The flag for whether to print debug information to the console.

See Also:
Constant Field Values

POOL_INIT

protected static final int POOL_INIT
The initial size of the context pool.

See Also:
Constant Field Values

POOL_INCR

protected static final int POOL_INCR
The increment of the context pool.

See Also:
Constant Field Values

FLAG_TYPEDEF

protected static final int FLAG_TYPEDEF
The flag for typedefs.

See Also:
Constant Field Values

FLAG_SCOPE

protected static final int FLAG_SCOPE
The flag for scopes.

See Also:
Constant Field Values

FLAG_TYPE_SPEC

protected static final int FLAG_TYPE_SPEC
The flag for having parsed a type specifier.

See Also:
Constant Field Values

FLAG_PARAMS

protected static final int FLAG_PARAMS
The flag for having parsed a function parameter list.

See Also:
Constant Field Values

FLAG_MODIFIED

protected static final int FLAG_MODIFIED
The flag for having modified the bindings.

See Also:
Constant Field Values

FLAG_STRUCTURE

protected static final int FLAG_STRUCTURE
The flag for structure/union declaration lists.

See Also:
Constant Field Values

top

protected CParserState.Context top
The top of the context stack. The implementation assumes that this field always references at least one context, which corresponds to the global namespace.


nesting

protected int nesting
The current nesting level.


annotation

protected Annotation annotation
The current annotation, if any.

Constructor Detail

CParserState

public CParserState()
Create a C parser state object.

Method Detail

fillPool

protected void fillPool(int n)
Add the specified number of fresh contexts to the pool.

Parameters:
n - The number to add.

takeFromPool

protected CParserState.Context takeFromPool()
Take a context from the pool, refilling the pool if necessary.

Returns:
A fresh context.

addToPool

protected void addToPool(CParserState.Context c)
Return the specified context to the pool, clearing it along the way.

Parameters:
c - The context to return.

push

protected void push(CParserState.Context c)
Push the specified context onto the context stack.

Parameters:
c - The context to push.

pop

protected CParserState.Context pop()
Pop a context from the context stack.

Returns:
The top-most context.

reset

public void reset(String file)
Specified by:
reset in interface State

start

public void start()
Specified by:
start in interface State

commit

public void commit()
Specified by:
commit in interface State

abort

public void abort()
Specified by:
abort in interface State

typedef

public void typedef()
Record a typedef storage class specifier.


parameters

public void parameters()
Record a function parameter list.


functionDeclarator

public void functionDeclarator()
Record a function declarator.


typeSpecifier

public void typeSpecifier()
Record a type specifier.


pushScope

public void pushScope()
Enter a new scope.


popScope

public void popScope()
Exit the last scope.


enterStructure

public void enterStructure()
Enter a structure declaration list.


exitStructure

public void exitStructure()
Exit a structure declaration list.


bind

public void bind(String id)
Implicitly bind the specified identifier. Depending on the current parsing context, the identifier is either bound as a type or as an object/function/constant.

Parameters:
id - The identifier.

bind

public void bind(String id,
                 boolean isType)
Explicitly bind the specified identifier.

Parameters:
id - The identifier.
isType - The flag for whether the identifier represents a type.

isType

public boolean isType(String id)
Determine whether the specified identifier names a type.

Parameters:
id - The identifier.
Returns:
true if the specified identifier names a type.

isValid

public boolean isValid(Node idl)
Determine whether a declaration actually is a declaration. This method determines whether the sequence
   DeclarationSpecifiers l:InitializedDeclaratorList?
 
can actually represent a declaration. It assumes that any type specifier encountered while parsing DeclarationSpecifiers has been marked through typeSpecifier().

Parameters:
idl - The result of parsing the optional initialized declarator list.
Returns:
true if the declaration is a declaration.

lineMarker

public void lineMarker(String file,
                       int line,
                       String isStartFile,
                       String isReturnToFile,
                       String isSystemHeader,
                       String isExternC,
                       Location location)
Record a line marker. Note that string values for the four flags are interpreted as follows: Any non-null string counts for true, while null counts for false.

Parameters:
file - The file name (without quotes).
line - The line number.
isStartFile - The start file flag.
isReturnToFile - The return to file flag.
isSystemHeader - The system header flag.
isExternC - The extern C flag.
location - The line marker's source location.
See Also:
LineMarker

pragma

public void pragma(String directive,
                   Location location)
Record a pragma.

Parameters:
directive - The actual directive.
location - The pragma's source location.
See Also:
Pragma

ident

public void ident(String ident,
                  Location location)
Record an ident directive.

Parameters:
ident - The actual identity marker.
location - The ident directive's source location.
See Also:
SourceIdentity

mark

public void mark()
Mark the current annotation. This method must be called before recognizing the nonterminals to be annotated. Furthermore, it must be called within the context of a stateful production.


annotate

public Node annotate(Node node)
Annotate the specified node. If any annotations have been recorded and marked, the specified node is wrapped by those annotations and the outer-most annotation is returned. Otherwise, the specified node is returned.

Parameters:
node - The node.
Returns:
The annotated node.


Copyright © 2008-2010 NYU Analysis of Computer Systems group. All Rights Reserved.