You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The *.y template file generated by BNFC using global varilables to store the parsed AST and internal status.
/* Global variables holding parse results for entrypoints. */ProcYY_RESULT_Proc_=0;
ListProcYY_RESULT_ListProc_=0;
This means the parser is not thread-safe.
Please consider enabling Thread Local Storage for them.
Here is a thread_local macro suggested here.
#ifndefthread_local# if__STDC_VERSION__ >= 201112&& !defined __STDC_NO_THREADS__# definethread_local _Thread_local
# elif defined _WIN32&& ( \
defined _MSC_VER|| \
defined __ICL|| \
defined __DMC__|| \
defined __BORLANDC__ )
# definethread_local __declspec(thread)
/* note that ICC (linux) and Clang are covered by __GNUC__ */# elif defined __GNUC__|| \
defined __SUNPRO_C|| \
defined __xlC__# definethread_local __thread
# else# error "Cannot define thread_local"
# endif#endif
I am not very proficient in C. Could you contribute a pull request?
Or, if you do not want to touch the Haskell code, could you describe precisely (e.g. by diffs) how the output of the C Backend should change to implement your feature?
if I run a single test case (means the test case call the C parser only once), everything works fine;
If I run my test cases, even used mutex to synchronize the calls to the C parser. It raises occasional errors. And the error changes, sometime it says syntax error in different location; sometimes it says memory error...
It seems BNFC C parser is not ready to be used in this way. So I give up this approach, and wrap the C parser into a child process. And everytime I need to parse a source file, launch the child process and exit after parsing the file.
The following changes are what I have attempted in *.y file.
First I added a macro in the top part.
#ifndefthread_local# if__STDC_VERSION__ >= 201112&& !defined __STDC_NO_THREADS__# definethread_local _Thread_local
# elif defined _WIN32&& ( \
defined _MSC_VER|| \
defined __ICL|| \
defined __DMC__|| \
defined __BORLANDC__ )
# definethread_local __declspec(thread)
/* note that ICC (linux) and Clang are covered by __GNUC__ */# elif defined __GNUC__|| \
defined __SUNPRO_C|| \
defined __xlC__# definethread_local __thread
# else# error "Cannot define thread_local"
# endif#endif
Then added thread_local before all global variables under the following line
/* Global variables holding parse results for entrypoints. */
thread_local ProcYY_RESULT_Proc_=0;
thread_local ListProcYY_RESULT_ListProc_=0;
...
Reset all global variables in the beginning of the parser method.
@wangjia184: Sorry, I hijacked this issue for one part of it: removing global variables from the C/C++ parser. Likely, the issue is not fixed for you with that. If you could provide me with a test scenario that I could run to chase the remaining problems, I could give it another try.
Concretely, I would need some self-contained test that runs parsers in parallel (as you seem to do) and exhibits the problems you report (syntax/memory errors).
Thanks @andreasabel for the efforts, I am willing to help. but since I am quite busy these days, dont have time to look into it.
I intend to attach a debugger to diagnose the issue. I may get back to you when I have something, but please expect soon about that
Activity
andreasabel commentedon Mar 19, 2021
I am not very proficient in C. Could you contribute a pull request?
Or, if you do not want to touch the Haskell code, could you describe precisely (e.g. by diffs) how the output of the C Backend should change to implement your feature?
wangjia184 commentedon Mar 24, 2021
Hi @andreasabel
It seems BNFC C parser is not ready to be used in this way. So I give up this approach, and wrap the C parser into a child process. And everytime I need to parse a source file, launch the child process and exit after parsing the file.
The following changes are what I have attempted in *.y file.
First I added a macro in the top part.
Then added
thread_localbefore all global variables under the following lineReset all global variables in the beginning of the parser method.
It just does not work and fail with occasional and different errors..
andreasabel commentedon Mar 24, 2021
With
bisonyou have to do some extra work to get a reentrant parser. https://www.gnu.org/software/bison/manual/html_node/Pure-Decl.htmlMaybe we could add change BNFC to generate reentrant parsers by default.
[-]Add thread_local to global variables in C parser[/-][+]Make C parser reentrant[/+][ re #349 ] C lexer: use flex's predefined start state INITIAL
[ re #349 ] C parser: code segment after %union, entrypoints in the end
[ #349 ] C parser: return parse result in parameter to yyparse
[ re #349 ] C: let bison generate Bison.h, to slim down Parser.h
[ fix #349 ] Make parser reentrant by using reentrant lexer
21 remaining items
[ re #349 ] C/C++ lexer: use flex's predefined start state INITIAL
[ re #349 ] C parser: code segment after %union, entrypoints in the end
[ #349 ] C parser: return parse result in parameter to yyparse
[ re #349 ] C: let bison generate Bison.h, to slim down Parser.h
[ fix #349 ] Make parser reentrant by using reentrant lexer
[ WIP #349 ] unify C and C++ lexer/parser: CPP/NoSTL part
[ WIP #349 ] unify C and C++ lexer/parser: CPP/STL part
[ #349 ] C++: remove clones of CFtoFlex/Bison
[ WIP #349 ] unify C and C++ lexer/parser: CPP/NoSTL part
[ WIP #349 ] unify C and C++ lexer/parser: CPP/STL part
[ #349 ] C++: remove clones of CFtoFlex/Bison
[ #349 ] CHANGELOG
andreasabel commentedon Apr 6, 2021
@wangjia184: Sorry, I hijacked this issue for one part of it: removing global variables from the C/C++ parser. Likely, the issue is not fixed for you with that. If you could provide me with a test scenario that I could run to chase the remaining problems, I could give it another try.
Concretely, I would need some self-contained test that runs parsers in parallel (as you seem to do) and exhibits the problems you report (syntax/memory errors).
wangjia184 commentedon Apr 6, 2021
Thanks @andreasabel for the efforts, I am willing to help. but since I am quite busy these days, dont have time to look into it.
I intend to attach a debugger to diagnose the issue. I may get back to you when I have something, but please expect soon about that