Flex & Bison output after yyparse possible?



  • Hallo Leute,

    ich nutze zur Zeit Flex & Bison um einen Compiler zu schreiben. Hierbei habe ich einen Timer der mir sagen soll wie lang er für das ganze gebraucht hat.

    Nun da ich weiß es fängt an wenn ich meine "Eingabe" getätigt hab und dann die "Enter-Taste" drücke (start Timer) sowie wenn der Parser mit der kompletten Interpretierung des letzten Tokens durch ist. (stop Timer).

    Soweit so gut, was ich also gerne hätte wäre ein start des Timers direkt vor dem "loslegen" des Parsers und wenn dieser mit allen Zeilen die er interpretieren soll durch ist soll dieser die Zeit stoppen und mir danach sofort ausgeben.

    So ungefähr...

    int main()
    {			
    	printf("\nICT - FLEX & BISON\nPlease enter a valid parameter line:\n\n");
    
    	yyparse();   // <-- start Timer right after input
    
    	// here stop Timer and print as result
    
    	...
    
    	printf("Results out of an object: %s",....);
    
    	printf("line: %8d\nwords: %8d\n chars: %8d\n", lines, words, chars);
    
    	system("pause");
    	return 0;
    }
    

    Problem hierbei ist das nach dem yyparse(); nichts mehr kommt. Sprich die folgenden Zeilen werden nicht mal mehr angerührt.

    Es gibt scheinbar noch die Möglichkeit von:

    int main(int argc, char* argv[])
    {
        while(1)
        {
            printf("lex:%d\r\n",yylex());
        }
    
        printf("Result...."); // <--- here do the output and show the elapsed time of the timer
    
        return 0;
    }
    
    extern "C" int yywrap();
    
    int yywrap ()
    {
        return 1;
    }
    

    jedoch kommt hier auch nichts am Schluss als zusätzliche Ausgabe...

    Hat irgendwer ne Ahnung ob ich eine solche Ausgabe nicht nur am Ende jeder Zeile als Token machen kann? Vllt gibt es ein #DEFINE dazu oder irgendeine Option die ich übersehen habe...

    Ich muss einfach an diesem yyparse() oder yylex() "vorbeikommen".

    Hoffe jemand weiß Rat 🙂
    Danke schonmal Adi



  • String als Ganzes einlesen, Timer starten, dem Parser den String uebergeben, Timer stoppen, Zeit ausgeben.



  • Das sagst du so leicht...

    String einlesen
    Timer starten
    an yyparse() übergeben (passiert ja automatisch?)

    alles kein Ding, jedoch

    Egal was ich mach ich komm ja aus dem yyparse() nicht mehr "zurück" um nachfolgende tätigkeiten abzuarbeiten...



  • Leider ist meine Erfahrung mit flex+bison schon 7 Jahre her und der Quelltext existiert nicht mehr. Warum kommst du aus yyparse() nicht mehr zurueck? Kann es daran liegen, dass du immer noch auf Input wartest, beispielsweise in yylex?



  • Passt schon 🙂 7 Jahre ist ja auch ne ganz schön lange Zeit.

    Ja ich denke so in der Art... jedoch nutze ich yylex() so in der main-function ja nicht 😕

    das waere mal mein kompletter Flex-Code, wie man sieht nutz ich in der "main" (eig. ictParse()) die yyparse() Funktion...

    /*
    R,UUT_1_R3,220kR;220kR,10%,230kR;12,13;G:45,99;W:10;M:10;O:1;D:1000,200mV;MTYPE:R_X2;IMAX:248mA;RX:367mR
    
    %option prefix="ict" noyywrap
    */
    
    %{
    	#include <stdio.h>
    	#include <stdlib.h>
    	#include <string.h>
    	#include "y.tab.h"
    	#include ".\..\Platform\CMeasureWrapper.h"
    
    	int chars = 0;
    	int words = 0;
    	int lines = 0;
    %}
    
    %s UUT_N UUT_NAME MTYPE_DESC IMAX_VALUE IMAX_UNIT
    
    %%
    
    ^R							{ words++; chars += strlen(yytext); return t_R_Measure; }
    ^Rx4						{ words++; chars += strlen(yytext); return t_Rx4_Measure; }
    ^C							{ words++; chars += strlen(yytext); return t_C_Measure; }
    ^L							{ words++; chars += strlen(yytext); return t_L_Measure; }
    UUT_						{
    								BEGIN UUT_N;
    								words++; 
    								chars += strlen(yytext);
    								return t_uut_;	
    							}
    <UUT_N>[0-9]+				{ 
    								BEGIN UUT_NAME;
    								yylval.i32_integer=atoi(yytext);
    								chars += strlen(yytext);
    								return tu_uut_n; 
    							}	
    <UUT_NAME>[\_][a-zA-Z0-9]+	{ 
    								BEGIN INITIAL;
    								yylval.ps_character=_strdup(yytext);
    								chars += strlen(yytext);
    								return ts_uut_name; 
    							}
    
    [\;]G[\:]					{ words++; chars += strlen(yytext); return t_guarding; }
    [\;]STIMU[\:]				{ words++; chars += strlen(yytext); return t_stimuly; }
    [\;]W[\:]					{ words++; chars += strlen(yytext); return t_wait; }
    [\;]M[\:]					{ words++; chars += strlen(yytext); return t_measureIntegration; }
    [\;]O[\:]					{ words++; chars += strlen(yytext); return t_offset; }
    [\;]SOLL[\:]				{ words++; chars += strlen(yytext); return t_soll; }
    [\;]D[\:]					{ words++; chars += strlen(yytext); return t_discharge; }
    [\;]MTYPE[\:]				{ 
    								BEGIN MTYPE_DESC;
    								words++; 
    								chars += strlen(yytext);
    								return t_measureType; 
    							}
    <MTYPE_DESC>[a-zA-Z0-9_]+	{
    								BEGIN INITIAL;
    								yylval.ps_character=_strdup(yytext);
    								chars += strlen(yytext);
    								return ts_measureType_Desc; 
    							}
    [\;]AC[\:]					{ words++; chars += strlen(yytext); return t_frequency; }
    [\;]CX[\:]					{ words++; chars += strlen(yytext); return t_capacity; }
    [\;]LX[\:]					{ words++; chars += strlen(yytext); return t_inductivity; }
    [\;]RX[\:]					{ words++; chars += strlen(yytext); return t_effResistance; }
    
    [\;]IMAX[\:]				{ BEGIN IMAX_VALUE; words++; chars += strlen(yytext); return t_maxVoltage; }
    <IMAX_VALUE>[0-9]+			{
    								BEGIN IMAX_UNIT;
    								yylval.i32_integer=atoi(yytext);
    								chars += strlen(yytext);
    								return tu_iMAX;
    							}
    <IMAX_VALUE>[0-9]+[\.][0-9]+ {
    								BEGIN IMAX_UNIT;
    								yylval.f64_float=atof(yytext); 
    								chars += strlen(yytext);
    								return tf_iMAX;
    							}
    <IMAX_UNIT>(mA|\n)			{
    								BEGIN INITIAL;
    								yylval.ps_character=_strdup(yytext);
    								chars += strlen(yytext);
    								return ts_iMaxUnit;
    							}
    (p|n|u|m|k|M)				{
    								yylval.ps_character=_strdup(yytext);
    								chars++;
    								return ts_prefix;
    							}
    F				 			{ chars++; return t_faradUnit; }
    R							{ chars++; return t_ohmUnit; }
    V							{ chars++; return t_voltageUnit; }
    A							{ chars++; return t_currentUnit; }
    %							{ chars++; return t_percentUnit; }
    [0-9]+ 	        			{
    								yylval.i32_integer=atoi(yytext); 
    								words++; chars += strlen(yytext);
    								return ti_number; 
    							}
    [0-9]+[\.][0-9]+   			{
    								yylval.f64_float=atof(yytext); 
    								words++; chars += strlen(yytext);
    								return tf_number; 
    							}
    [\;]						{ chars++; return t_areaBorder; }
    [\,]						{
    								yylval.ps_character=_strdup(yytext);
    								chars++;
    								return ts_logicalOperator; 
    							}
    [a-zA-Z]?					{
    								yylval.ps_character=_strdup(yytext);
    								words++; chars += strlen(yytext);
    								return ts_word;
    							}
    .							{	chars++; return t_anyChar; }
    
    "\n"						{	chars++; lines++; return LASTSIGN; }
    (quit|Quit|QUIT|exit|Exit|EXIT|bye|stopp|ciao)	{	return QUIT; }
    
    %%
    
    int yywrap()
    {
    	return 1;
    }
    
    void ictParse()
    {
    // Start parsing
    	yyparse();		// <-- it never reachs beyond that point
    
    // Print Results
    	IMeasurement *po_ictValues = NULL;		// <-- so not even here...
    	po_ictValues = getCurrentMeasureObject();
    	printf("Results: uut_n(%u)", po_ictValues->getResult_UUT_N());
    }
    
    int main()
    {		
    	printf("\nICT - FLEX & BISON\nPlease enter a valid parameter line:\n\n");
    
    	ictParse();
    
    	printf("line: %8d\nwords: %8d\n chars: %8d\n", lines, words, chars);
    
    	system("pause");
    	return 0;
    }
    

    Kennt jemand ne andere Möglichkeit, nachdem alles durchgeprased wurde, einen Art Output zu tätigen?



  • Okkkay ich hab grade noch ein paar Änderungen gemacht die mein Flex-File eigentlich nicht sonderlich stark verändert hat, musste paar Sachen reduzieren da diese nicht mehr geparsed werden müssen -> rausgelöscht)

    Nun bekomm ich einen Output von nach dem yyparse(), jedoch nur im Error-Fall beim call von YYERROR; .

    Was wiederum bedeutet ich brauch am Ende von allem was geparsed werden soll eine erkennung das nun alles fertig ist um durch dieses Symbol dann sagen zu können, "hey jetzt ist alles durch".
    Somit kann ich dann halt folgendes machen.

    Bison.y

    ...
    EndSymbol:
    	specialEndingSymbol
    	{
    		printf("done, output after yyparse() is comin...");
    		YYACCEPT; // <--- force Bison to stop and print the results.
    	};
    ...
    

    Mein Flex-File sieht nun so aus...

    /*
    R,UUT_1_R3,220kR;220kR,10%,230kR;12,13;G:45,99;W:10;M:10;O:1;D:1000,200mV;MTYPE:R_X2;
    
    %option prefix="ict" noyywrap
    */
    
    %{
    	#include <stdio.h>
    	#include <stdlib.h>
    	#include <string.h>
    	#include "y.tab.h"
    	#include ".\..\Platform\CMeasureWrapper.h"
    
    	int chars = 0;
    	int words = 0;
    	int lines = 0;
    %}
    
    %s UUT_N UUT_NAME MTYPE_DESC
    
    %%
    
    ^R							{ words++; chars += strlen(yytext); return t_R_Measure; }
    ^C							{ words++; chars += strlen(yytext); return t_C_Measure; }
    ^L							{ words++; chars += strlen(yytext); return t_L_Measure; }
    UUT_						{
    								BEGIN UUT_N;
    								words++; 
    								chars += strlen(yytext);
    								return t_uut_;	
    							}
    <UUT_N>[0-9]+				{ 
    								BEGIN UUT_NAME;
    								yylval.i32_integer=atoi(yytext);
    								chars += strlen(yytext);
    								return tu_uut_n; 
    							}	
    <UUT_NAME>[\_][a-zA-Z0-9]+	{ 
    								BEGIN INITIAL;
    								yylval.ps_character=_strdup(yytext);
    								chars += strlen(yytext);
    								return ts_uut_name; 
    							}
    
    [\;]G[\:]					{ words++; chars += strlen(yytext); return t_guarding; }
    [\;]STIMU[\:]				{ words++; chars += strlen(yytext); return t_stimuly; }
    [\;]W[\:]					{ words++; chars += strlen(yytext); return t_wait; }
    [\;]M[\:]					{ words++; chars += strlen(yytext); return t_measureIntegration; }
    [\;]O[\:]					{ words++; chars += strlen(yytext); return t_offset; }
    [\;]SOLL[\:]				{ words++; chars += strlen(yytext); return t_soll; }
    [\;]D[\:]					{ words++; chars += strlen(yytext); return t_discharge; }
    [\;]MTYPE[\:]				{ 
    								BEGIN MTYPE_DESC;
    								words++; 
    								chars += strlen(yytext);
    								return t_measureType; 
    							}
    <MTYPE_DESC>[a-zA-Z0-9_]+	{
    								BEGIN INITIAL;
    								yylval.ps_character=_strdup(yytext);
    								chars += strlen(yytext);
    								return ts_measureType_Desc; 
    							}
     /*	[\;]AC[\:]					{ words++; chars += strlen(yytext); return t_frequency; } */
    [\;]CX[\:]					{ words++; chars += strlen(yytext); return t_capacity; }
    [\;]LX[\:]					{ words++; chars += strlen(yytext); return t_inductivity; }
    [\;]RX[\:]					{ words++; chars += strlen(yytext); return t_effResistance; }
    (p|n|u|m|k|M)				{
    								yylval.ps_character=_strdup(yytext);
    								chars++;
    								return ts_prefix;
    							}
    F				 			{ chars++; return t_faradUnit; }
    R							{ chars++; return t_ohmUnit; }
    V							{ chars++; return t_voltageUnit; }
    A							{ chars++; return t_currentUnit; }
    %							{ chars++; return t_percentUnit; }
    [0-9]+ 	        			{
    								yylval.i32_integer=atoi(yytext); 
    								words++; chars += strlen(yytext);
    								return ti_number; 
    							}
    [0-9]+[\.][0-9]+   			{
    								yylval.f64_float=atof(yytext); 
    								words++; chars += strlen(yytext);
    								return tf_number; 
    							}
    [\;]						{ chars++; return t_areaBorder; }
    [\,]						{
    								yylval.ps_character=_strdup(yytext);
    								chars++;
    								return ts_logicalOperator; 
    							}
    [a-zA-Z]?					{
    								yylval.ps_character=_strdup(yytext);
    								words++; chars += strlen(yytext);
    								return ts_word;
    							}
    .							{	chars++; return t_anyChar; }
    
    "\n"						{	chars++; lines++; return LASTSIGN; }
    (quit|Quit|QUIT|exit|Exit|EXIT|bye|stop|ciao)	{	return QUIT; }
    
    %%
    
    int yywrap()
    {
    	return 1;
    }
    
    void ictParse()
    {
    // Start parsing
    	yyparse();
    
    // Print Results
    	IMeasurement *po_ictValues = NULL;
    	po_ictValues = getCurrentMeasureObject();
    	//printf("Results: uut_n(%u)", po_ictValues->getResult_UUT_N());
    }
    
    int main()
    {		
    	printf("\nICT - FLEX & BISON\nPlease enter a valid parameter line:\n\n");
    
    	ictParse();
    
    	printf("line: %8d\nwords: %8d\n chars: %8d\n", lines, words, chars);
    
    	system("pause");
    	return 0;
    }
    

    Hoffe diese kleine aber feine Lösung hilft nicht nur mir 🙂 Jedoch komisch das es zuvor nicht geklappt hat... Hatte diese Art Weg zuvor schonmals versucht... But now it works somehow 😃

    peace Adi


Anmelden zum Antworten