Search code examples
tokenjavacc

How to match a variable to a token in javacc?


I am trying to match a variable (a string) to one of my defined tokens in JAVACC. The pseudocode for what I am trying to do is...

String x;
if (x matches <FUNCTIONNAME>) {...}

How would I go about achieving this?

Thank you


Solution

  • Here is one way to do it. Use the STATIC==false option. The following code should do what you need

    public boolean matches( String str, int k ) {
    // Precondition:  k should be one of the integers
    //   given a name in XXXConstants 
    // Postcondition: result is true if and only if str would be lexed by
    // the lexer as a single token of kind k possibly
    // preceeded and followed by any number of skipped and special tokens.
        StringReader sr = new StringReader( str ) ;
        SimpleCharStream scs = new SimpleCharStream( sr ) ;
        XXXTokenManager lexer = new XXXTokenManager( scs );
    
        boolean matches = false ;
        try  { 
            Token a = lexer.getNextToken() ;
            Token b = lexer.getNextToken() ;
            matches = a.kind == k && b.kind == 0 ; }
        catch( Throwable t ) {}
        return matches ; 
    }
    

    One problem with this is that it will skip tokens declared as SKIP or SPECIAL_TOKEN. E.g. if I use a Java lexer then "/*hello*/\tworld // \n" will still match JavaParserConstants.ID. If you don't want this, you need to do two things. First go into the .jj file and convert any SKIP tokens to SPECIAL_TOKENS. Second add checks that there no special tokens were found

    matches = a.kind == k && b.kind == 0 && a.specialToken == null && b.specialToken == null ;