2021-09-21

Sep 22 In-Class Exercise Thread .

Please post your solutions to the Sep 22 In-Class Exercise to this thread.

Best,

Chris

Please post your solutions to the Sep 22 In-Class Exercise to this thread. Best, Chris
2021-09-22

-- Sep 22 In-Class Exercise Thread

<pre>From lextest.c: #line 2 "lextest.c"

#line 4 "lextest.c"

#define YY_INT_ALIGNED short int

/* A lexical scanner generated by flex */

Code: %{ #include <stdio.h> int wordCount = 0; %} word a|(the) %% [\t\n ]+ {printf("I see whitespace\n");} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; } </pre>

<pre>From lextest.c: #line 2 "lextest.c" #line 4 "lextest.c" #define YY_INT_ALIGNED short int /* A lexical scanner generated by flex */ Code: %{ #include <stdio.h> int wordCount = 0; %} word a|(the) %% [\t\n ]+ {printf("I see whitespace\n");} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; } </pre>

-- Sep 22 In-Class Exercise Thread

%{ #include &lt;stdio.h&gt; int articleCount = 0; %} article [^a\s\t\n]+ article [^the\s\t\n]+ %% [\t\n ]+ {printf(&quot;I see &#039;a&#039; or &#039;the&#039;\n&quot;);} //what to do if see pattern {article} {articleCount++;} %% int main() { yylex(); //calls the lexer printf(&quot;article count: %d&quot;, articleCount); return 0; }

(Edited: 2021-09-22)
%{ #include <stdio.h> int articleCount = 0; %} article [^a\s\t\n]+ article [^the\s\t\n]+ %% [\t\n ]+ {printf("I see 'a' or 'the'\n");} //what to do if see pattern {article} {articleCount++;} %% int main() { yylex(); //calls the lexer printf("article count: %d", articleCount); return 0; }

-- Sep 22 In-Class Exercise Thread

&lt;pre&gt; code from lex.yy.c:

#line 2 &quot;lex.yy.c&quot;

#define YY_INT_ALIGNED short int

/* A lexical scanner generated by flex */

#define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 6 #define YY_FLEX_SUBMINOR_VERSION 4 #if YY_FLEX_SUBMINOR_VERSION &gt; 0 #define FLEX_BETA #endif

inclass.l code:

%{ #include &lt;stdio.h&gt; int wordCount = 0; %} word a|the %% [\t\n ]+ {printf(&quot;I see whitespace\n&quot;);} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; } To compile: lex -o lextest.c lextest.l #default output is lex.yy.c gcc lextest.c -o lextest -ll #-ll not needed if use flex. &lt;/pre&gt;

(Edited: 2021-09-22)
<pre> code from lex.yy.c: #line 2 "lex.yy.c" #define YY_INT_ALIGNED short int /* A lexical scanner generated by flex */ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 6 #define YY_FLEX_SUBMINOR_VERSION 4 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif inclass.l code: %{ #include <stdio.h> int wordCount = 0; %} word a|the %% [\t\n ]+ {printf("I see whitespace\n");} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; } To compile: lex -o lextest.c lextest.l #default output is lex.yy.c gcc lextest.c -o lextest -ll #-ll not needed if use flex. </pre>

User Icon
-- Sep 22 In-Class Exercise Thread

%{ #include &lt;stdio.h&gt; int wordCount = 0; %} word a|the %% [\t\n ]+ {printf(&quot;I see whitespace\n&quot;);} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; }

(Edited: 2021-09-22)
%{ #include <stdio.h> int wordCount = 0; %} word a|the %% [\t\n ]+ {printf("I see whitespace\n");} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; }

-- Sep 22 In-Class Exercise Thread

%{ include &lt;stdio.h&gt; int wordCount = 0; %} word a|the %% [\t\n ]+ {printf(&quot;I see whitespace\n&quot;);} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; }

%{ include <stdio.h> int wordCount = 0; %} word a|the %% [\t\n ]+ {printf("I see whitespace\n");} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; }

-- Sep 22 In-Class Exercise Thread

lextest.l :

%{ #include &lt;stdio.h&gt; int wordCount = 0; %} word a|the %% [\t\n ]+ {printf(&quot;I see an &#039;a&#039; or a &#039;the&#039; \n&quot;);} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; }

lextest.c :

#line 2 &quot;lextest.c&quot;

#line 4 &quot;lextest.c&quot;

#define YY_INT_ALIGNED short int

/* A lexical scanner generated by flex */

#define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 #define YY_FLEX_SUBMINOR_VERSION 35 #if YY_FLEX_SUBMINOR_VERSION &gt; 0 #define FLEX_BETA #endif

(Edited: 2021-09-22)
lextest.l : %{ #include <stdio.h> int wordCount = 0; %} word a|the %% [\t\n ]+ {printf("I see an 'a' or a 'the' \n");} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; } lextest.c : #line 2 "lextest.c" #line 4 "lextest.c" #define YY_INT_ALIGNED short int /* A lexical scanner generated by flex */ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 #define YY_FLEX_SUBMINOR_VERSION 35 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif

-- Sep 22 In-Class Exercise Thread

%{ #include &lt;stdio.h&gt; int wordCount = 0; %} word (a|the) %% [\t\n ]+ {printf(&quot;I see whitespace\n&quot;);} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; }

%{ #include <stdio.h> int wordCount = 0; %} word (a|the) %% [\t\n ]+ {printf("I see whitespace\n");} {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; }

-- Sep 22 In-Class Exercise Thread

#line 2 &quot;lex.yy.c&quot;

#line 4 &quot;lex.yy.c&quot;

#define YY_INT_ALIGNED short int

/* A lexical scanner generated by flex */

#define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 #define YY_FLEX_SUBMINOR_VERSION 35 #if YY_FLEX_SUBMINOR_VERSION &gt; 0 #define FLEX_BETA #endif

%{ #include &lt;stdio.h&gt; int wordCount = 0; %} word [^a\s\t\n]+|[^the\s\t\n]+ %% [\t\n ]+ {printf(&quot;I see whitespace\n&quot;);} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;word count: %d&quot;, wordCount); return 0; }

(Edited: 2021-09-22)
#line 2 "lex.yy.c" #line 4 "lex.yy.c" #define YY_INT_ALIGNED short int /* A lexical scanner generated by flex */ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 #define YY_FLEX_SUBMINOR_VERSION 35 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif %{ #include <stdio.h> int wordCount = 0; %} word [^a\s\t\n]+|[^the\s\t\n]+ %% [\t\n ]+ {printf("I see whitespace\n");} //what to do if see pattern {word} {wordCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("word count: %d", wordCount); return 0; }

-- Sep 22 In-Class Exercise Thread

&lt;nowiki&gt;lextest.l:

{% #include &lt;stdio.h&gt; int aTheCount = 0; %} word a|(the); %% {word} {aTheCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf(&quot;occurrences of &#039;a&#039; and &#039;the&#039;: %d&quot;, aTheCount); return 0; }&lt;/nowiki&gt;

&lt;nowiki&gt;result:

&gt; lex lextest.l &gt; gcc lex.yy.c -ll -o example &gt; ./example &gt; the girl saw a dog the girl sw dog occurrences of &#039;a&#039; and &#039;the&#039;: 2&lt;/nowiki&gt;

(Edited: 2021-09-22)
<nowiki>lextest.l: {% #include <stdio.h> int aTheCount = 0; %} word a|(the); %% {word} {aTheCount++;} %% int main() { yylex(); //call the lexer. Gets input from command line until ^D printf("occurrences of 'a' and 'the': %d", aTheCount); return 0; }</nowiki> <nowiki>result: > lex lextest.l > gcc lex.yy.c -ll -o example > ./example > the girl saw a dog the girl sw dog occurrences of 'a' and 'the': 2</nowiki>
[ Next ]
X