Search code examples
f#lexical-analysisfslex

FsLex - Differ between 2 strings


I've a couple of tokens:

PNAME and ENAME - both are strings.

Now I want to setup 2 rules in my lexer, in order to match those tokens.

The first rule (PNAME) should match when the string consist of characters a-z, and optional special characters @/().

The second rule (ENAME) should match when the string consist of characters a-z, and an optional prefix (#/.).

Now how would I make up a rule in my lexer file that only would match ENAME - Even when theres no prefix?

If it makes any difference, then ENAME will have a { after it's string like: (prefix)eName { - However this bracket shouldn't be passed into the parser...

Any suggestions?


Solution

  • If this question is related to your previous question (about parsing CSS) files, then you should probably use a different approach.

    The lexer should only identify simple tokens such as # and . (token names HASH and DOT), curly braces (tokens LCURLY and RCURLY for { and } respectively) and any identifier IDENT using regular expression that takes any sequence of characters a-zA-Z.

    The rest of the processing (such as identifying CSS rules .foo { ... }) should be done in the parser. In my previous answer, I described how to parse a list of property names - it assumes that you have navigators which is a syntactic element that specifies the HTML elements such as #name or #name .class. You can write separate parsing rules for these:

    navigators = 
      | navigator            { [$1] }
      | navigator navigators { $1::$2 }
    navigator = 
      | HASH IDENT   { SelectByID($2) }
      | DOT IDENT    { SelectByClass($1) }
    

    For more information about wirting parsers & lexers see the wikibooks article and Chris Smith's blog.