Dashboard > USF Computer Science 652 - Programming Languages > ... > Symbol tables > Configuration file symbol references solution
  USF Computer Science 652 - Programming Languages Log In | Sign Up   View a printable version of the current page.  
  Configuration file symbol references solution
Added by Terence Parr, last edited by Terence Parr on Feb 18, 2008  (view change)
Labels: 
(None)

Most data file formats require very little in terms of symbol table management. Indeed this problem only requires a list or hash table. As you read in the list of site specs:

site main { ... }
site demo { ... }
site meditation { ... }

make a HashMap called sites or whatever that maps:

mainSiteContext object 1
demoSiteContext object 2
meditationSiteContext object 3

When you see the family spec's sites definition:

...
family jguru {
  sites = { main, meditation };
  ...
}

You can test sites.get("main") and sites.get("meditation") to see if they exist. If not, flag an error.

In terms of implementation, you would see the following defs:

site
    :    'site' ID '{'
             {
             SiteContext sx=new SiteContext($ID.text);
             sites.put($ID.text, sx);
             }
             siteElement[sx]+
         '}'
    ;

siteElement[SiteContext sx] // rule fills in sx
    :    ... {sx.setXXX(...);} ...
    ;

After the site specs, you would parse the family spec and check the references:

family
    :    'family' ID '{' element+ '}'
    ;
element
    :    sites
    |    ...
    ;
sites
    :    'sites' '=' '{'
             ID {sites.get($ID.text)!=null}? (',' ID {sites.get($ID.text)!=null}?)*
         '}'
    ;

Note the use of a validating semantic predicate to test whether the ID is a valid site spec.

In summary: you need a HashMap and a bit of code to save the site specs and a bit of code to reference the HashMap.

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators