0
votes

I was just wondering how any of you guys would implement multi character variables in c using Flex and Bison / Lex and Yacc ? Any if so can you provide maybe a simple example?

I am attempting to write an interpreter for a language and I can't seem to find a good way to implement variables, so far the methods I've tried have either failed or causing the execution of any program with a lot of variables become really so (I mean it could take minutes to execute a program that just assigns 1000 variables and does nothing else)

Thanks for your time, Francis

1
Be careful when tagging. Flex is used for the Adobe/Apache UI framework. Flex-lexer is used for the lexical analyzer. I fixed the tag.JeffryHouser
@Reboog711 Flex has been around since -97, adobe flex since 2004 so it is really should be the other way around: Flex for the lexer and Adobe Flex for whatever that is.nic
@nic I'm not the one who created the tags; but the Flex tag gets lots of questions about the UI framework and very few about the lexical analyzer.JeffryHouser

1 Answers

0
votes

In a lexer provided by ADAIC for Ada the following method is used, i find it ver useful for lexing multu-character literals such as reserved words and variables. It (along with corresponding Bison grammar and some other stuff) is available at ADAIC docs

%%
[a-zA-Z](_?[a-zA-Z0-9])* return(lk_keyword(yytext));
%%
# define NUM_KEYWORDS  69
KEY_TABLE key_tab[NUM_KEYWORDS] = 
{
{"ABORT",       ABORT},
{"ABS",         ABS},
....
....
....
};


lk_keyword(str)
        char *str;
 {
        int min;
        int max;
        int guess, compare;

        min = 0;
        max = NUM_KEYWORDS-1;
        guess = (min + max) / 2;
        to_upper(str);

        for (guess=(min+max)/2; min<=max; guess=(min+max)/2) {
                if ((compare = strcmp(key_tab[guess].kw, str)) < 0) {
                        min = guess + 1;
                } else if (compare > 0) {
                        max = guess - 1;
                } else {
                        return key_tab[guess].kwv;
        }
    }
        return identifier;
 }