How to handle extremely long string in flex and bison
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to handle extremely long string in flex and bison
Hi,
I am writing a parser using flex and bison. Some input files having extremely long token and causing segmentation fault while parsing.
I am setting the YYLMAX value to a very large number and allocate the maximum size for the string as the YYLMAX value as well. I think the value that I set has exceed the buffer limit for lexer thus it does not really solve the issue.
Any idea on how to handle extremely long token in flex and bison?
Try to include the `%pointer' directive in the first section of flex input. In this case yytext will grow automatically.
From `info flex':
Quote:
Note that `yytext' can be defined in two different ways: either as a
character _pointer_ or as a character _array_. You can control which
definition `flex' uses by including one of the special directives
`%pointer' or `%array' in the first (definitions) section of your flex
input. The default is `%pointer', unless you use the `-l' lex
compatibility option, in which case `yytext' will be an array. The
advantage of using `%pointer' is substantially faster scanning and no
buffer overflow when matching very large tokens (unless you run out of
dynamic memory). The disadvantage is that you are restricted in how
your actions can modify `yytext' (*note Actions::), and calls to the
`unput()' function destroys the present contents of `yytext', which can
be a considerable porting headache when moving between different `lex'
versions.
The advantage of `%array' is that you can then modify `yytext' to
your heart's content, and calls to `unput()' do not destroy `yytext'
(*note Actions::). Furthermore, existing `lex' programs sometimes
access `yytext' externally using declarations of the form:
extern char yytext[];
This definition is erroneous when used with `%pointer', but correct
for `%array'.
I just realized that %pointer is the default behavior in flex. So you either run out of memory or use lex instead of flex, or there are bugs in the code. Can you provide more info? Example is trivial:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.