0% found this document useful (0 votes)
39 views23 pages

Universidad Autónoma Del Estado de Hidalgo: Semestre y Grupo: 6°1

This document contains code for 9 exercises related to lex and yacc programs. The code implements lexical analyzers using lex to count characters, words, and lines in files, handle command line arguments, and recognize tokens in different parser states.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views23 pages

Universidad Autónoma Del Estado de Hidalgo: Semestre y Grupo: 6°1

This document contains code for 9 exercises related to lex and yacc programs. The code implements lexical analyzers using lex to count characters, words, and lines in files, handle command line arguments, and recognize tokens in different parser states.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIVERSIDAD AUTÓNOMA DEL

ESTADO DE HIDALGO

LICENCIATURA EN CIENCIAS
COMPUTACIONALES

INSTITUTO DE CIENCIAS BASICAS E


INGENIERIA

“AUTOMATAS Y COMPILADORES”

Semestre y grupo: 6°1

Alumno: Gutiérrez Martinez Roberto Alejandro


Código ejercicio 1

%option noyywrap

%{

#include<stdio.h>

%}

%%

[\n\t] ;

-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {printf("number\n");}

. ECHO;

%%

int main()

yylex();

return 0;

}
Captura ejercicio 1
Código ejercicio 2
%option noyywrap

%{

#include<stdio.h>

unsigned charCount = 0, wordCount = 0, lineCount = 0;

%}

word [^ \t\n]+

eol \n

%%

{word} {wordCount++; charCount += yyleng; }

{eol} {charCount++; lineCount++; }

. charCount++;

%%

int main(argc,argv)

int argc;

char **argv;

if(argc > 1){

FILE *file;

file =fopen(argv[1], "r");

if(!file) {

fprintf(stderr, "Could not open %s\n", argv[1]);

exit(1);

yyin = file;

yylex();

printf("%u %u %u\n", charCount, wordCount, lineCount);


return 0;

}
Captura ejercicio 2

Captura ejercicio 2
Código ejercicio 3

%{

#include<stdio.h>

unsigned long charCount = 0, wordCount = 0, lineCount = 0;

#undef yywrap /*sometimes a macro by default */

%}

word [^ \t\n]+

eol \n

%%

{word} {wordCount++; charCount += yyleng; }

{eol} {charCount++; lineCount; }

. charCount++;

%%

char **fileList;

unsigned currentFile = 0;

unsigned nFiles;

unsigned long totalCC = 0;

unsigned long totalWC = 0;

unsigned long totalLC = 0;

int main(argc, argv)

int argc;

char **argv;

FILE *file;
fileList = argv+1;

nFiles = argc-1;

if(argc == 2){

/*

* we handle the single file case differently from

* the multiple file case since we don't need to

* print a summary line

*/

currentFile = 1;

file = fopen(argv[1], "r");

if(!file){

fprintf(stderr, "Could not open %s\n", argv[1]);

exit(1);

yyin = file;

if (argc > 2)

yywrap(); /* open first file */

yylex();

/*

* once again, we handle zero or one file

* differently from multiple files.

*/

if(argc > 2){

printf("%8lu %8lu %s\n", lineCount, wordCount, charCount,


fileList[currentFile-1]);

totalCC += charCount;
totalWC += wordCount;

totalLC += lineCount;

printf("%8lu %8lu %8lu total\n", totalLC, totalWC, totalCC);

} else

printf("%8lu %8lu %8lu\n", lineCount, wordCount, charCount);

return 0;

yywrap()

FILE *file = NULL;

if((currentFile != 0) && (nFiles > 1) && (currentFile < nFiles)){

/*

* we print out the statistics for the previous file.

*/

printf("%8lu %8lu %8lu %s\n", lineCount, wordCount, charCount,


fileList[currentFile-1]);

totalCC += charCount;

totalWC += wordCount;

totalLC += lineCount;

charCount = wordCount = lineCount = 0;

fclose (yyin); /* done with that file */

while (fileList[currentFile] != (char *)0) {

file = fopen(fileList[currentFile++], "r");

if(file != NULL){

yyin = file;

break;

}
fprintf(stderr, "Could not open %s\n", fileList[currentFile-1]);

return(file ? 0 : 1); /* 0 means there's more input */

}
Captura ejercicio 3
Código ejercicio 4
%option noyywrap
%{
#include<stdio.h>
unsigned verbose;
char *progName;
%}

%%
-h |
"-?" |
-help { printf("usage is: %s [-help | -h | -? ] [-verbose | -v] "
"[(-file| -f) filename]\n", progName);
}
-v |
-verbose {printf("verbose mode is on\n"); verbose =1;}
%%
main(argc, argv)
int argc;
char **argv;
{
progName =*argv;
yylex();

return 0;
}

Captura ejercicio 4
Codigo ejercicio 8
%option noyywrap

%{

#include<stdio.h>

#undef input

#undef unput

int input(void);

unsigned verbose;

char *progName;

%}

%%

-h |

"-?" |

-help {printf("usage is: %s [-help | -h | -? ][-verbose | -v]" " [(-file| -f) filename]\n", progName);}

-v |

-verbose {printf("verbose mode is on\n"); verbose = 1;}

%%

int main(int argc, char **argv)

progName = *argv;

targv = argv+1;

arglim = argv+argc;

yylex();

static unsigned offset = 0;

int
input(void)

char c;

if(targv >= arglim)

return(0); /* EOF */

/* end of argument, moveto the next */

if ((c = targv[0][offset++]) != '\0')

return(c);

targv++;

offset = 0;

return(' ');

/* simple unput only backs up, doesn't allow you to */

/* put back different text */

void

unput(int ch)

/* AT&T lex sometimes puts back the EOF ! */

if(ch == 0)

return; /* ignore, can't put back EOF */

if(offset){

offset--;

return;

targv--; /* back to previous arg */

offset = strlen(*targv);

}
Codigo ejercicio 6
%option noyywrap

%{

#include<stdio.h>

#undef input

#undef unput

unsigned verbose;

unsigned fname;

char *progName;

%}

%s FNAME

%%

[ ]+ /* ignore blanks */

-h |

"-?" |

-help {printf("usage is: %s [-help | -h | -? ] [-verbose | -v]" " (-file | -f) filename\n", progName);}

-f |

-file {BEGIN FNAME; fname = 1; }

<FNAME>[^ ]+ { printf("use file %s\n", yytext); BEGIN 0; fname = 2;}

[^ ]+ ECHO;

%%

char **targv; /* remembers arguments */

char **arglim; /* end of arguments */

int main(int argc, char **argv)

{
progName = *argv;

targv = argv+1;

arglim = argv+argc;

yylex();

if(fname < 2)

printf("No filename given\n");

}
Captura Ejercicio 6
Código ejercicio 7

%option noyywrap

%{

#include<stdio.h>

%}

%s MAGIC

%%

<MAGIC>.+ { BEGIN 0; printf("Magic: "); ECHO; }

magic BEGIN MAGIC;

%%

int main()

yylex();

return 0;

}
Captura ejercicio 7
Codigo Ejercicio 8

%option noyywrap

%{

#include<stdio.h>

int comments, code, whiteSpace;

%}

%x COMMENT

%%

^[ \t]*"/*" {BEGIN COMMENT; /* enter comment eating state */}

^[ \t]*"/*".*"*/"[ \t]*\n { comments++; /* self-contained comment */ }

<COMMENT>"*/"[ \t]*\n { BEGIN 0; comments++;}

<COMMENT>"*/"

<COMMENT>\n { comments++; }

<COMMENT>.\n { comments++; }

^[ \t]*\n {whiteSpace++; }

.+"/*".*"*/".*\n { code++; }

.*"/*".*"*/".+\n { code++; }

.+"/*".*\n { code++; BEGIN COMMENT; }

.\n { code++; }

. ; /* ignore everything else */

%%

int main()

yylex();

printf("code: %d, comments %d, whitespace %d\n", code, comments, whiteSpace);


Codigo Ejercicio 9

%option noyywrap

%{

#include<stdio.h>

int comments, code, whiteSpace;

%}

%x COMMENT

%%

^[ \t]*"/*" {BEGIN COMMENT; /* enter comment eating state */}

^[ \t]*"/*".*"*/"[ \t]*\n { comments++; /* self-contained comment */ }

<COMMENT>"*/"[ \t]*\n { BEGIN 0; comments++;}

<COMMENT>"*/"

<COMMENT>\n { comments++; }

<COMMENT>.\n { comments++; }

^[ \t]*\n {whiteSpace++; }

.+"/*".*"*/".*\n { code++; }

.*"/*".*"*/".+\n { code++; }

.+"/*".*\n { code++; BEGIN COMMENT; }

.\n { code++; }

. ; /* ignore everything else */

%%

int main()

yylex();

printf("code: %d, comments %d, whitespace %d\n", code, comments, whiteSpace);

}
Captura ejercicio 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy