%{
#include < stdio. h>
#include < ctype. h>
#include < string. h>
#include < stdbool. h>
#define MAX_TOKEN_LENGTH 100
// Token types
typedef enum {
MOT_CLE,
INT,
IDENTIFICATEUR,
OPERATEUR,
SEPARATEUR,
CHAINE_CARACTERE,
ERROR
} TokenType;
// Keywords list
const char* keywords[ ] = { "si", "sino", "is", "tq", "qt", "rpt", "jsq", "lre", "ecr", "vrai", "faux"} ;
int num_keywords = sizeof( keywords) / sizeof( keywords[ 0 ] ) ;
// Check if a token
is a keyword
bool is_keyword( const char* token) {
for ( int i = 0 ; i < num_keywords; i++ ) {
if ( strcasecmp( token, keywords[ i] ) == 0 ) {
}
}
return false;
}
// Write token to output file
void process_token( TokenType type, int line_number, const char* token) {
switch ( type) {
case MOT_CLE:
fprintf( yyout, "Mot_Cle %d %s\n", line_number, token);
break;
case INT:
fprintf( yyout, "Int %d %s\n", line_number, token);
break;
fprintf
( yyout
, "
Float %d %s\n", line_number, token); break;
case IDENTIFICATEUR:
fprintf( yyout, "Identificateur %d %s\n", line_number, token);
break;
case OPERATEUR:
fprintf( yyout, "Operateur %d %s\n", line_number, token);
break;
case SEPARATEUR:
fprintf( yyout, "Separateur %d %s\n", line_number, token);
break;
case CHAINE_CARACTERE:
fprintf( yyout, "Chaine_Caracteres %d %s\n", line_number, token);
break;
case ERROR:
fprintf( yyout, "ERROR %d %s\n", line_number, token);
break;
}
}
int line_number = 1 ;
%}
%%
"// ". * { /* Skip single-line comments */
// Do nothing
}
"/*" { fprintf(yyout, "ERROR %d Début de commentaire multiligne\n", line_number);
bool in_multiline_comment = true;
while (in_multiline_comment) {
int token = yylex();
if (token == 0) {
fprintf(yyout, "ERROR %d Fin de fichier dans un commentaire\n", line_number);
return;
}
if (yytext[0] == '*' && yytext[1] == '/') {
in_multiline_comment = false;
fprintf(yyout, "ERROR %d Fin de commentaire multiligne\n", line_number);
}
}
}
"\""[^"\\]*(\\.[^"\\]*)*"\" { /* Handle string literals */
yytext[ yyleng - 1 ] = '\0' ; // Remove trailing quote
process_token( CHAINE_CARACTERE, line_number, yytext + 1 ) ; // Skip leading quote
}
[ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ?( [ eE] [ +- ] ?[ 0 - 9 ] + ) ? { /* Handle integers and floats */
process_token
( FLOAT , line_number
, yytext
) ; }
[ a- zA- Z] [ a- zA- Z0- 9 ] * { /* Handle identifiers and keywords */
if ( is_keyword( yytext) ) {
process_token( MOT_CLE, line_number, yytext) ;
} else {
process_token( IDENTIFICATEUR, line_number, yytext) ;
}
}
[ + \- */ =<> !&| : ] { /* Handle operators */
if ( strcmp( yytext, "+ ") == 0 || strcmp( yytext, "- ") == 0 ||
strcmp( yytext, "* ") == 0 || strcmp( yytext, "/ ") == 0 ||
strcmp( yytext, "= ") == 0 || strcmp( yytext, "< ") == 0 ||
strcmp( yytext, "> ") == 0 || strcmp( yytext, "! ") == 0 ||
strcmp( yytext, "& ") == 0 || strcmp( yytext, "| ") == 0 ||
strcmp( yytext, ": ") == 0 ) {
process_token( OPERATEUR, line_number, yytext) ;
} else if ( strcmp( yytext, "== ") == 0 || strcmp( yytext, "! = ") == 0 ||
strcmp( yytext, "<= ") == 0 || strcmp( yytext, ">= ") == 0 ||
strcmp( yytext, "&& ") == 0 || strcmp( yytext, "|| ") == 0 ) {
process_token( OPERATEUR, line_number, yytext) ;
}
}
[ ;, ( ) ] { /* Handle separators */
process_token( SEPARATEUR, line_number, yytext) ;
}
[ \t\r] + { /* Skip whitespace */
// Do nothing
}
\n { /* Increment line number */
line_number++ ;
}
. { /* Handle invalid characters */
process_token( ERROR, line_number, yytext) ;
}
%%
int main( int argc, char** argv) {
if ( argc > 1 ) {
yyin = fopen( argv[ 1 ] , "r") ;
if ( ! yyin) {
perror( "Error opening input file") ;
return 1 ;
}
}
yyout = fopen( "output. txt", "w") ;
if ( ! yyout) {
perror( "Error opening output file") ;
if ( yyin) fclose( yyin) ;
return 1 ;
}
yylex( ) ;
if ( yyin) fclose( yyin) ;
if ( yyout) fclose( yy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YXNlIE1PVF9DTEU6CiAgICAgICAgICAgIGZwcmludGYoeXlvdXQsICJNb3RfQ2xlICVkICVzXG4iLCBsaW5lX251bWJlciwgdG9rZW4pOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIElOVDoKICAgICAgICAgICAgZnByaW50Zih5eW91dCwgIkludCAlZCAlc1xuIiwgbGluZV9udW1iZXIsIHRva2VuKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSBGTE9BVDoKICAgICAgICAgICAgZnByaW50Zih5eW91dCwgIkZsb2F0ICVkICVzXG4iLCBsaW5lX251bWJlciwgdG9rZW4pOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIElERU5USUZJQ0FURVVSOgogICAgICAgICAgICBmcHJpbnRmKHl5b3V0LCAiSWRlbnRpZmljYXRldXIgJWQgJXNcbiIsIGxpbmVfbnVtYmVyLCB0b2tlbik7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgT1BFUkFURVVSOgogICAgICAgICAgICBmcHJpbnRmKHl5b3V0LCAiT3BlcmF0ZXVyICVkICVzXG4iLCBsaW5lX251bWJlciwgdG9rZW4pOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIFNFUEFSQVRFVVI6CiAgICAgICAgICAgIGZwcmludGYoeXlvdXQsICJTZXBhcmF0ZXVyICVkICVzXG4iLCBsaW5lX251bWJlciwgdG9rZW4pOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIENIQUlORV9DQVJBQ1RFUkU6CiAgICAgICAgICAgIGZwcmludGYoeXlvdXQsICJDaGFpbmVfQ2FyYWN0ZXJlcyAlZCAlc1xuIiwgbGluZV9udW1iZXIsIHRva2VuKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSBFUlJPUjoKICAgICAgICAgICAgZnByaW50Zih5eW91dCwgIkVSUk9SICVkICVzXG4iLCBsaW5lX251bWJlciwgdG9rZW4pOwogICAgICAgICAgICBicmVhazsKICAgIH0KfQoKaW50IGxpbmVfbnVtYmVyID0gMTsKJX0KCiUlCgoiLy8iLiogICAgICAgICAgeyAvKiBTa2lwIHNpbmdsZS1saW5lIGNvbW1lbnRzICovCiAgICAgICAgICAgICAgICAgIC8vIERvIG5vdGhpbmcKICAgICAgICAgICAgICAgIH0KCiIvKiIgICAgICAgICAgICB7IGZwcmludGYoeXlvdXQsICJFUlJPUiAlZCBEw6lidXQgZGUgY29tbWVudGFpcmUgbXVsdGlsaWduZVxuIiwgbGluZV9udW1iZXIpOwogICAgICAgICAgICAgICAgICBib29sIGluX211bHRpbGluZV9jb21tZW50ID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGluX211bHRpbGluZV9jb21tZW50KSB7CiAgICAgICAgICAgICAgICAgICAgICBpbnQgdG9rZW4gPSB5eWxleCgpOwogICAgICAgICAgICAgICAgICAgICAgaWYgKHRva2VuID09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBmcHJpbnRmKHl5b3V0LCAiRVJST1IgJWQgRmluIGRlIGZpY2hpZXIgZGFucyB1biBjb21tZW50YWlyZVxuIiwgbGluZV9udW1iZXIpOwogICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGlmICh5eXRleHRbMF0gPT0gJyonICYmIHl5dGV4dFsxXSA9PSAnLycpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBpbl9tdWx0aWxpbmVfY29tbWVudCA9IGZhbHNlOwogICAgICAgICAgICAgICAgICAgICAgICAgIGZwcmludGYoeXlvdXQsICJFUlJPUiAlZCBGaW4gZGUgY29tbWVudGFpcmUgbXVsdGlsaWduZVxuIiwgbGluZV9udW1iZXIpOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgoiXCIiW14iXFxdKihcXC5bXiJcXF0qKSoiXCIgeyAvKiBIYW5kbGUgc3RyaW5nIGxpdGVyYWxzICovCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB5eXRleHRbeXlsZW5nIC0gMV0gPSAnXDAnOyAvLyBSZW1vdmUgdHJhaWxpbmcgcXVvdGUKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByb2Nlc3NfdG9rZW4oQ0hBSU5FX0NBUkFDVEVSRSwgbGluZV9udW1iZXIsIHl5dGV4dCArIDEpOyAvLyBTa2lwIGxlYWRpbmcgcXVvdGUKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQoKWzAtOV0rKFwuWzAtOV0rKT8oW2VFXVsrLV0/WzAtOV0rKT8geyAvKiBIYW5kbGUgaW50ZWdlcnMgYW5kIGZsb2F0cyAqLwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJvY2Vzc190b2tlbihGTE9BVCwgbGluZV9udW1iZXIsIHl5dGV4dCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KClthLXpBLVpdW2EtekEtWjAtOV0qIHsgLyogSGFuZGxlIGlkZW50aWZpZXJzIGFuZCBrZXl3b3JkcyAqLwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaXNfa2V5d29yZCh5eXRleHQpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcm9jZXNzX3Rva2VuKE1PVF9DTEUsIGxpbmVfbnVtYmVyLCB5eXRleHQpOwogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJvY2Vzc190b2tlbihJREVOVElGSUNBVEVVUiwgbGluZV9udW1iZXIsIHl5dGV4dCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CgpbK1wtKi89PD4hJnw6XSAgIHsgLyogSGFuZGxlIG9wZXJhdG9ycyAqLwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoc3RyY21wKHl5dGV4dCwgIisiKSA9PSAwIHx8IHN0cmNtcCh5eXRleHQsICItIikgPT0gMCB8fAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIioiKSA9PSAwIHx8IHN0cmNtcCh5eXRleHQsICIvIikgPT0gMCB8fAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIj0iKSA9PSAwIHx8IHN0cmNtcCh5eXRleHQsICI8IikgPT0gMCB8fAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIj4iKSA9PSAwIHx8IHN0cmNtcCh5eXRleHQsICIhIikgPT0gMCB8fAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIiYiKSA9PSAwIHx8IHN0cmNtcCh5eXRleHQsICJ8IikgPT0gMCB8fAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIjoiKSA9PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcm9jZXNzX3Rva2VuKE9QRVJBVEVVUiwgbGluZV9udW1iZXIsIHl5dGV4dCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoc3RyY21wKHl5dGV4dCwgIj09IikgPT0gMCB8fCBzdHJjbXAoeXl0ZXh0LCAiIT0iKSA9PSAwIHx8CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIjw9IikgPT0gMCB8fCBzdHJjbXAoeXl0ZXh0LCAiPj0iKSA9PSAwIHx8CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyY21wKHl5dGV4dCwgIiYmIikgPT0gMCB8fCBzdHJjbXAoeXl0ZXh0LCAifHwiKSA9PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcm9jZXNzX3Rva2VuKE9QRVJBVEVVUiwgbGluZV9udW1iZXIsIHl5dGV4dCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CgpbOywoKV0gICAgICAgICAgICB7IC8qIEhhbmRsZSBzZXBhcmF0b3JzICovCiAgICAgICAgICAgICAgICAgICAgICAgIHByb2Nlc3NfdG9rZW4oU0VQQVJBVEVVUiwgbGluZV9udW1iZXIsIHl5dGV4dCk7CiAgICAgICAgICAgICAgICAgICAgfQoKWyBcdFxyXSsgICAgICAgICAgeyAvKiBTa2lwIHdoaXRlc3BhY2UgKi8KICAgICAgICAgICAgICAgICAgICAgIC8vIERvIG5vdGhpbmcKICAgICAgICAgICAgICAgICAgICB9CgpcbiAgICAgICAgICAgICAgICB7IC8qIEluY3JlbWVudCBsaW5lIG51bWJlciAqLwogICAgICAgICAgICAgICAgICAgICAgbGluZV9udW1iZXIrKzsKICAgICAgICAgICAgICAgICAgICB9CgouICAgICAgICAgICAgICAgICB7IC8qIEhhbmRsZSBpbnZhbGlkIGNoYXJhY3RlcnMgKi8KICAgICAgICAgICAgICAgICAgICAgIHByb2Nlc3NfdG9rZW4oRVJST1IsIGxpbmVfbnVtYmVyLCB5eXRleHQpOwogICAgICAgICAgICAgICAgICAgIH0KCiUlCgppbnQgbWFpbihpbnQgYXJnYywgY2hhcioqIGFyZ3YpIHsKICAgIGlmIChhcmdjID4gMSkgewogICAgICAgIHl5aW4gPSBmb3Blbihhcmd2WzFdLCAiciIpOwogICAgICAgIGlmICgheXlpbikgewogICAgICAgICAgICBwZXJyb3IoIkVycm9yIG9wZW5pbmcgaW5wdXQgZmlsZSIpOwogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICB9CgogICAgeXlvdXQgPSBmb3Blbigib3V0cHV0LnR4dCIsICJ3Iik7CiAgICBpZiAoIXl5b3V0KSB7CiAgICAgICAgcGVycm9yKCJFcnJvciBvcGVuaW5nIG91dHB1dCBmaWxlIik7CiAgICAgICAgaWYgKHl5aW4pIGZjbG9zZSh5eWluKTsKICAgICAgICByZXR1cm4gMTsKICAgIH0KCiAgICB5eWxleCgpOwoKICAgIGlmICh5eWluKSBmY2xvc2UoeXlpbik7CiAgICBpZiAoeXlvdXQpIGZjbG9zZSh5eW91dCk7CgogICAgcmV0dXJuIDA7Cn0=