Take the 2-minute tour ×

Hi i'm trying to let lex/yacc split this string

table subwayLines:int[3]

into tokens of table, subwayLines, int[3] with the [3] optional(i.e. int or int[3])

everything is fine until i try to recognize the "int", so this is what i did in lex

[A-Za-z0-9\[\]]+    { /* column property*/ 
                   yylval.sval = (char *)strdup(yytext);
           char* temp=yylval.sval;
            return STRING;

i know the problem is in


because when i changed it to


it works except I still can't go without the "[" or "]", for example, if i wrote this in my string:

table subwayLines:int

then it gives me a syntax error so does anyone knows how to do change it? thanks

share|improve this question
Is there a reason to recognize int[3] as a string token, rather than to return that as four tokens and parse the syntax? What if there is whitespace (is that allowed?) I.e. int [ 3 ]. –  Kaz Mar 19 '12 at 19:49

1 Answer 1

To make the [3] optional, this will not work:


You've made only the square brackets optional, but not the number in between. You need something like


I.e. the entire square-bracketed part is optional.

Also the combination (REGEX)?+ doesn't make much sense (the ?+ part of it). It's equivalent to (REGEX)*, since you're effectively saying that (REGEX) is optional, one or more times, which is like zero or more.

(Not sure why you have the optional comma in the second example; the first one doesn't recognize a comma and it's not shown in your example input.)

share|improve this answer

Your Answer


