How can i peek next token in flex without removing it from buffer

Lisu

I'm trying to build lexer for some subset of AMPL language. I need to now what type of symbolic name lexer is dealing right now. Every symbolic name is var or param or set. Fortunately all of them have to be declared before they are used. So i thought i can use lookahead operator in flex simply changing code in lexer from

SYMBOLIC_NAME [a-zA-Z_][a-zA-Z0-9_]*
%%
param             { return PARAM; }
var               { return VAR; }
set               { return SET; }
{SYMBOLIC_NAME}   { yylval.string = (char*) strdup(yytext);
                    return SYMBOLIC_NAME;  
                  }
%%

to some thing like this

SYMBOLIC_NAME [a-zA-Z_][a-zA-Z0-9_]*
%{
 #include <vector>
 #include <algorithm>
 std::vector<std::string> paramNames;
 std::vector<std::string> setNames;
 std::vector<std::string> varNames;

%}
%%
param/(.|\n)+{SYMBOLIC_NAME}             { paramNames.push_back(&yytext[5]);
                                           return PARAM; }
var/(.|\n)+{SYMBOLIC_NAME}               { varNames.push_back(&yytext[3]);
                                           return VAR; }
set/(.|\n)+{SYMBOLIC_NAME}               { setNames.push_back(&yytext[3]);
                                           return SET; }
{SYMBOLIC_NAME}   { if ( std::find(setNames.begin(), setNames.end(), yytext) != setNames.end() ) {
                    yylval.string = (char*) strdup(yytext);
                    return SET_NAME;
                    }

                   if ( std::find(paramNames.begin(), paramNames.end(), yytext) != paramNames.end() ){
                    yylval.string = (char*) strdup(yytext);
                    return PARAM_NAME;  
                  }
                  if ( std::find(varNames.begin(), varNames.end(), yytext) != varNames.end() ){
                    yylval.string = (char*) strdup(yytext);
                    return VAR_NAME;  
                  }
               }
%%

I know it's not going to work because yytext does not contain second part of first three regexps. And question appears how can i peek what is under (.|\n)+{SYMBOLIC_NAME} .

PS

I know the code is not optimal but it is not an issue here :D

aghast

I think you're trying to check a symbol table for what kind of name you are seeing.

If that is the case, you should do this by communicating through the symbol table. That is:

  1. Create a simple "symbol" rule. Your original rule is fine:

    {SYMBOLIC_NAME}   { yylval.string = (char*) strdup(yytext);
                    return SYMBOLIC_NAME;  
                  }
    
  2. Handle the declaration syntax at the parser level:

    var_decl : VAR SYMBOLIC_NAME
         { add name to symbol table }
    
  3. Now go back and extend your SYMBOLIC_NAME rule to check for defined symbols:

    {SYMBOLIC_NAME} {
                 yylval.string = (char*) strdup(yytext);
    
                 if ( std::find(setNames.begin(), setNames.end(), yytext) != setNames.end() ) {
                     return SET_NAME;
                 }
                 else if (... varNames ...) {
                     return VAR_NAME;
                 else if (... paramNames ...) {
                     return PARAM_NAME;
                 }
                 else {
                     return SYMBOLIC_NAME;
                 }
             }
    

Now you have one Flex target returning four possible tokens, depending on defined-ness. But Flex doesn't have to worry about remembering what symbol definition is active - you can let the parser handle that.

On the parser side, you write different rules:

var_decl: VAR SYMBOLIC_NAME
set_decl: SET SYMBOLIC_NAME

expr: atom '+' atom
atom: VAR_NAME | SET_NAME | PARAM_NAME

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I speed up extraction of the proportion of land cover types in a buffer from a raster?

来自分类Dev

How can I set the jenkins authentication token?

来自分类Dev

How can I change a word in a buffer using elisp?

来自分类Dev

How to remove a directory without removing its content?

来自分类Dev

How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

来自分类Dev

How can i stamp strings in c without errors?

来自分类Dev

How can I duplicate files and folders without content?

来自分类Dev

How can I return a value to a superclass without overriding the parent function?

来自分类Dev

How can I reinstall Windows 7 on a laptop without the drivers CD?

来自分类Dev

How can I extract text from images?

来自分类Dev

How can I see a Web API bearer token before it's returned?

来自分类Dev

How to remove a jquery UI element without removing elements?

来自分类Dev

如何根据Peekable :: peek的结果调用Peekable :: next?

来自分类Dev

Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

来自分类Dev

<canvas>: Remove the 2d context from memory without removing the webgl context from memory

来自分类Dev

How can I get the Mercurial Changeset ID from a revision number?

来自分类Dev

How can I load an image from a Drupal field to UIImageView?

来自分类Dev

How can I request an Image from a URL when the Response is 302?

来自分类Dev

How can I stop controls from using a KeyEvent?

来自分类Dev

How can I "slide up" a view on android from the bottom of a screen?

来自分类Dev

How can I prevent a window from being resized with tkinter?

来自分类Dev

How can I stop myself from using 'git commit -a'?

来自分类Dev

How do I rename multiple files by removing everything but numbers?

来自分类Dev

Mercurial: How can a I make a snapshot of my working directory without doing a changeset?

来自分类Dev

How can i run cmd as administrator privileges by cmd command without using any shortcut of cmd?

来自分类Dev

How can I click space to pause the video without down the page in JavaScript

来自分类Dev

Can someone explain how I got a Windows EFI error message without trying to install Windows?

来自分类Dev

Can someone explain how I got a Windows EFI error message without trying to install Windows?

来自分类Dev

How to pass data from one view to the next?

Related 相关文章

  1. 1

    How can I speed up extraction of the proportion of land cover types in a buffer from a raster?

  2. 2

    How can I set the jenkins authentication token?

  3. 3

    How can I change a word in a buffer using elisp?

  4. 4

    How to remove a directory without removing its content?

  5. 5

    How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

  6. 6

    How can i stamp strings in c without errors?

  7. 7

    How can I duplicate files and folders without content?

  8. 8

    How can I return a value to a superclass without overriding the parent function?

  9. 9

    How can I reinstall Windows 7 on a laptop without the drivers CD?

  10. 10

    How can I extract text from images?

  11. 11

    How can I see a Web API bearer token before it's returned?

  12. 12

    How to remove a jquery UI element without removing elements?

  13. 13

    如何根据Peekable :: peek的结果调用Peekable :: next?

  14. 14

    Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

  15. 15

    <canvas>: Remove the 2d context from memory without removing the webgl context from memory

  16. 16

    How can I get the Mercurial Changeset ID from a revision number?

  17. 17

    How can I load an image from a Drupal field to UIImageView?

  18. 18

    How can I request an Image from a URL when the Response is 302?

  19. 19

    How can I stop controls from using a KeyEvent?

  20. 20

    How can I "slide up" a view on android from the bottom of a screen?

  21. 21

    How can I prevent a window from being resized with tkinter?

  22. 22

    How can I stop myself from using 'git commit -a'?

  23. 23

    How do I rename multiple files by removing everything but numbers?

  24. 24

    Mercurial: How can a I make a snapshot of my working directory without doing a changeset?

  25. 25

    How can i run cmd as administrator privileges by cmd command without using any shortcut of cmd?

  26. 26

    How can I click space to pause the video without down the page in JavaScript

  27. 27

    Can someone explain how I got a Windows EFI error message without trying to install Windows?

  28. 28

    Can someone explain how I got a Windows EFI error message without trying to install Windows?

  29. 29

    How to pass data from one view to the next?

热门标签

归档