This document provides an overview of the Lex lexical analyzer tool. It discusses how Lex works, the format of Lex source programs, regular expressions in Lex, and Lex actions. Key points include:
- Lex uses regular expressions to match strings in input and convert them to tokens. It generates a C program (lex.yy.c) that performs this task.
- A Lex source program contains regular expressions, actions to perform on matches, and optional user-defined subroutines. It is separated into sections by '%%' delimiters.
- Regular expressions in Lex support operators, character classes, repetitions, and other features to define token patterns. Matched strings are accessed via predefined variables like yytext.
-
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
Using recycled concrete aggregates (RCA) for pavements is crucial to achieving sustainability. Implementing RCA for new pavement can minimize carbon footprint, conserve natural resources, reduce harmful emissions, and lower life cycle costs. Compared to natural aggregate (NA), RCA pavement has fewer comprehensive studies and sustainability assessments.
6th International Conference on Machine Learning & Applications (CMLA 2024)ClaraZara1
6th International Conference on Machine Learning & Applications (CMLA 2024) will provide an excellent international forum for sharing knowledge and results in theory, methodology and applications of on Machine Learning & Applications.
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...Amil Baba Dawood bangali
Contact with Dawood Bhai Just call on +92322-6382012 and we'll help you. We'll solve all your problems within 12 to 24 hours and with 101% guarantee and with astrology systematic. If you want to take any personal or professional advice then also you can call us on +92322-6382012 , ONLINE LOVE PROBLEM & Other all types of Daily Life Problem's.Then CALL or WHATSAPP us on +92322-6382012 and Get all these problems solutions here by Amil Baba DAWOOD BANGALI
#vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore#blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #blackmagicforlove #blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #Amilbabainuk #amilbabainspain #amilbabaindubai #Amilbabainnorway #amilbabainkrachi #amilbabainlahore #amilbabaingujranwalan #amilbabainislamabad
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
HEAP SORT ILLUSTRATED WITH HEAPIFY, BUILD HEAP FOR DYNAMIC ARRAYS.
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.
We have compiled the most important slides from each speaker's presentation. This year’s compilation, available for free, captures the key insights and contributions shared during the DfMAy 2024 conference.
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...ssuser7dcef0
Power plants release a large amount of water vapor into the
atmosphere through the stack. The flue gas can be a potential
source for obtaining much needed cooling water for a power
plant. If a power plant could recover and reuse a portion of this
moisture, it could reduce its total cooling water intake
requirement. One of the most practical way to recover water
from flue gas is to use a condensing heat exchanger. The power
plant could also recover latent heat due to condensation as well
as sensible heat due to lowering the flue gas exit temperature.
Additionally, harmful acids released from the stack can be
reduced in a condensing heat exchanger by acid condensation. reduced in a condensing heat exchanger by acid condensation.
Condensation of vapors in flue gas is a complicated
phenomenon since heat and mass transfer of water vapor and
various acids simultaneously occur in the presence of noncondensable
gases such as nitrogen and oxygen. Design of a
condenser depends on the knowledge and understanding of the
heat and mass transfer processes. A computer program for
numerical simulations of water (H2O) and sulfuric acid (H2SO4)
condensation in a flue gas condensing heat exchanger was
developed using MATLAB. Governing equations based on
mass and energy balances for the system were derived to
predict variables such as flue gas exit temperature, cooling
water outlet temperature, mole fraction and condensation rates
of water and sulfuric acid vapors. The equations were solved
using an iterative solution technique with calculations of heat
and mass transfer coefficients and physical properties.
4. PLLab, NTHU,Cs2403 Programming
Languages
4
What is Lex?
• The main job of a lexical analyzer
(scanner) is to break up an input stream
into more usable elements (tokens)
a = b + c * d;
ID ASSIGN ID PLUS ID MULT ID SEMI
• Lex is an utility to help you rapidly
generate your scanners
5. PLLab, NTHU,Cs2403 Programming
Languages
5
Lex – Lexical Analyzer
• Lexical analyzers tokenize input streams
• Tokens are the terminals of a language
– English
• words, punctuation marks, …
– Programming language
• Identifiers, operators, keywords, …
• Regular expressions define
terminals/tokens
6. PLLab, NTHU,Cs2403 Programming
Languages
6
Lex Source Program
• Lex source is a table of
– regular expressions and
– corresponding program fragments
digit [0-9]
letter [a-zA-Z]
%%
{letter}({letter}|{digit})* printf(“id: %sn”, yytext);
n printf(“new linen”);
%%
main() {
yylex();
}
7. PLLab, NTHU,Cs2403 Programming
Languages
7
Lex Source to C Program
• The table is translated to a C program
(lex.yy.c) which
– reads an input stream
– partitioning the input into strings which
match the given expressions and
– copying it to an output stream if necessary
9. PLLab, NTHU,Cs2403 Programming
Languages
9
(optional)
(required)
Lex Source
• Lex source is separated into three sections by %
% delimiters
• The general format of Lex source is
• The absolute minimum Lex program is thus
{definitions}
%%
{transition rules}
%%
{user subroutines}
%%
10. PLLab, NTHU,Cs2403 Programming
Languages
10
Lex v.s. Yacc
• Lex
– Lex generates C code for a lexical analyzer, or
scanner
– Lex uses patterns that match strings in the input
and converts the strings to tokens
• Yacc
– Yacc generates C code for syntax analyzer, or
parser.
– Yacc uses grammar rules that allow it to analyze
tokens from Lex and create a syntax tree.
13. PLLab, NTHU,Cs2403 Programming
Languages
13
Lex Regular Expressions
(Extended Regular Expressions)
• A regular expression matches a set of strings
• Regular expression
– Operators
– Character classes
– Arbitrary character
– Optional expressions
– Alternation and grouping
– Context sensitivity
– Repetitions and definitions
14. PLLab, NTHU,Cs2403 Programming
Languages
14
Operators
“ [ ] ^ - ? . * + | ( ) $ / { } % < >
• If they are to be used as text characters, an
escape should be used
$ = “$”
= “”
• Every character but blank, tab (t), newline (n)
and the list above is always a text character
15. PLLab, NTHU,Cs2403 Programming
Languages
15
Character Classes []
• [abc] matches a single character, which may
be a, b, or c
• Every operator meaning is ignored except -
and ^
• e.g.
[ab] => a or b
[a-z] => a or b or c or … or z
[-+0-9] => all the digits and the two signs
[^a-zA-Z] => any character which is not a
letter
16. PLLab, NTHU,Cs2403 Programming
Languages
16
Arbitrary Character .
• To match almost character, the
operator character . is the class of all
characters except newline
• [40-176] matches all printable
characters in the ASCII character set,
from octal 40 (blank) to octal 176
(tilde~)
17. PLLab, NTHU,Cs2403 Programming
Languages
17
Optional & Repeated
Expressions
• a? => zero or one instance of a
• a* => zero or more instances of a
• a+ => one or more instances of a
• E.g.
ab?c => ac or abc
[a-z]+ => all strings of lower case letters
[a-zA-Z][a-zA-Z0-9]* => all
alphanumeric strings with a leading
alphabetic character
18. PLLab, NTHU,Cs2403 Programming
Languages
18
Precedence of Operators
• Level of precedence
– Kleene closure (*), ?, +
– concatenation
– alternation (|)
• All operators are left associative.
• Ex: a*b|cd* = ((a*)b)|(c(d*))
19. PLLab, NTHU,Cs2403 Programming
Languages
19
Pattern Matching Primitives
Metacharacter Matches
. any character except newline
n newline
* zero or more copies of the preceding expression
+ one or more copies of the preceding expression
? zero or one copy of the preceding expression
^ beginning of line / complement
$ end of line
a|b a or b
(ab)+ one or more copies of ab (grouping)
[ab] a or b
a{3} 3 instances of a
“a+b” literal “a+b” (C escapes still work)
20. PLLab, NTHU,Cs2403 Programming
Languages
20
Recall: Lex Source
• Lex source is a table of
– regular expressions and
– corresponding program fragments (actions)
…
%%
<regexp> <action>
<regexp> <action>
…
%%
%%
“=“ printf(“operator: ASSIGNMENT”);
a = b + c;
a operator: ASSIGNMENT b + c;
21. PLLab, NTHU,Cs2403 Programming
Languages
21
Transition Rules
• regexp <one or more blanks> action (C code);
• regexp <one or more blanks> { actions (C code) }
• A null statement ; will ignore the input (no
actions)
[ tn] ;
– Causes the three spacing characters to be ignored
a = b + c;
d = b * c;
↓ ↓
a=b+c;d=b*c;
22. PLLab, NTHU,Cs2403 Programming
Languages
22
Transition Rules (cont’d)
• Four special options for actions:
|, ECHO;, BEGIN, and REJECT;
• | indicates that the action for this rule is from
the action for the next rule
– [ tn] ;
– “ “ |
“t” |
“n” ;
• The unmatched token is using a default
action that ECHO from the input to the output
24. PLLab, NTHU,Cs2403 Programming
Languages
24
Lex Predefined Variables
• yytext -- a string containing the lexeme
• yyleng -- the length of the lexeme
• yyin -- the input stream pointer
– the default input of default main() is stdin
• yyout -- the output stream pointer
– the default output of default main() is stdout.
• cs20: %./a.out < inputfile > outfile
• E.g.
[a-z]+ printf(“%s”, yytext);
[a-z]+ ECHO;
[a-zA-Z]+ {words++; chars += yyleng;}
25. PLLab, NTHU,Cs2403 Programming
Languages
25
Lex Library Routines
• yylex()
– The default main() contains a call of yylex()
• yymore()
– return the next token
• yyless(n)
– retain the first n characters in yytext
• yywarp()
– is called whenever Lex reaches an end-of-file
– The default yywarp() always returns 1
26. PLLab, NTHU,Cs2403 Programming
Languages
26
Review of Lex Predefined
Variables
Name Function
char *yytext pointer to matched string
int yyleng length of matched string
FILE *yyin input stream pointer
FILE *yyout output stream pointer
int yylex(void) call to invoke lexer, returns token
char* yymore(void) return the next token
int yyless(int n) retain the first n characters in yytext
int yywrap(void) wrapup, return 1 if done, 0 if not done
ECHO write matched string
REJECT go to the next alternative rule
INITAL initial start condition
BEGIN condition switch start condition
27. PLLab, NTHU,Cs2403 Programming
Languages
27
User Subroutines Section
• You can use your Lex routines in the same
ways you use routines in other programming
languages.
%{
void foo();
%}
letter [a-zA-Z]
%%
{letter}+ foo();
%%
…
void foo() {
…
}
28. PLLab, NTHU,Cs2403 Programming
Languages
28
User Subroutines Section
(cont’d)
• The section where main() is placed
%{
int counter = 0;
%}
letter [a-zA-Z]
%%
{letter}+ {printf(“a wordn”); counter++;}
%%
main() {
yylex();
printf(“There are total %d wordsn”, counter);
}
29. PLLab, NTHU,Cs2403 Programming
Languages
29
Usage
• To run Lex on a source file, type
lex scanner.l
• It produces a file named lex.yy.c which is
a C program for the lexical analyzer.
• To compile lex.yy.c, type
cc lex.yy.c –ll
• To run the lexical analyzer program, type
./a.out < inputfile
30. PLLab, NTHU,Cs2403 Programming
Languages
30
Versions of Lex
• AT&T -- lex
http://www.combo.org/lex_yacc_page/lex.html
• GNU -- flex
http://www.gnu.org/manual/flex-2.5.4/flex.html
• a Win32 version of flex :
http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html
or Cygwin :
http://sources.redhat.com/cygwin/
• Lex on different machines is not created equal.
32. PLLab, NTHU,Cs2403 Programming
Languages
32
Introduction
• What is YACC ?
– Tool which will produce a parser for a
given grammar.
– YACC (Yet Another Compiler Compiler)
is a program designed to compile a
LALR(1) grammar and to produce the
source code of the syntactic analyzer of
the language produced by this grammar.
33. PLLab, NTHU,Cs2403 Programming
Languages
33
How YACC Works
a.out
File containing desired
grammar in yacc format
yacc program
yacc program
C source program created by yacc
C compiler
C compiler
Executable program that will parse
grammar given in gram.y
gram.y
yacc
y.tab.c
cc
or gcc
34. PLLab, NTHU,Cs2403 Programming
Languages
34
yacc
How YACC Works
(1) Parser generation time
YACC source (*.y)
y.tab.h
y.tab.c
C compiler/linker
(2) Compile time
y.tab.c a.out
a.out
(3) Run time
Token stream
Abstract
Syntax
Tree
y.output
35. PLLab, NTHU,Cs2403 Programming
Languages
35
An YACC File Example
%{
#include <stdio.h>
%}
%token NAME NUMBER
%%
statement: NAME '=' expression
| expression { printf("= %dn", $1); }
;
expression: expression '+' NUMBER { $$ = $1 + $3; }
| expression '-' NUMBER { $$ = $1 - $3; }
| NUMBER { $$ = $1; }
;
%%
int yyerror(char *s)
{
fprintf(stderr, "%sn", s);
return 0;
}
int main(void)
{
yyparse();
return 0;
}
38. PLLab, NTHU,Cs2403 Programming
Languages
38
YACC File Format
%{
C declarations
%}
yacc declarations
%%
Grammar rules
%%
Additional C code
– Comments enclosed in /* ... */ may appear in
any of the sections.
42. PLLab, NTHU,Cs2403 Programming
Languages
42
Rules Section
• Normally written like this
• Example:
expr : expr '+' term
| term
;
term : term '*' factor
| factor
;
factor : '(' expr ')'
| ID
| NUM
;
43. PLLab, NTHU,Cs2403 Programming
Languages
43
The Position of Rules
expr : expr '+' term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term : term '*' factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor : '(' expr ')' { $$ = $2; }
| ID
| NUM
;
44. PLLab, NTHU,Cs2403 Programming
Languages
44
The Position of Rules
expr : expr
expr '+' term { $$ = $1 + $3; }
| term
term { $$ = $1; }
;
term : term
term '*' factor { $$ = $1 * $3; }
| factor
factor { $$ = $1; }
;
factor : '(
(' expr ')' { $$ = $2; }
| ID
| NUM
;
$1
$1
45. PLLab, NTHU,Cs2403 Programming
Languages
45
The Position of Rules
expr : expr '+
+' term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term : term '*
*' factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor : '(' expr
expr ')' { $$ = $2; }
| ID
| NUM
; $2
$2
46. PLLab, NTHU,Cs2403 Programming
Languages
46
The Position of Rules
expr : expr '+' term
term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term : term '*' factor
factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor : '(' expr ')
)' { $$ = $2; }
| ID
| NUM
; $3
$3 Default: $$ = $1;
Default: $$ = $1;
49. PLLab, NTHU,Cs2403 Programming
Languages
49
Communication between LEX
and YACC
%{
#include <stdio.h>
#include "y.tab.h"
%}
id [_a-zA-Z][_a-zA-Z0-9]*
%%
int { return INT; }
char { return CHAR; }
float { return FLOAT; }
{id} { return ID;}
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token CHAR, FLOAT, ID, INT
%%
yacc -d xxx.y
Produced
y.tab.h:
# define CHAR 258
# define FLOAT 259
# define ID 260
# define INT 261
parser.y
scanner.l
50. PLLab, NTHU,Cs2403 Programming
Languages
50
YACC
• Rules may be recursive
• Rules may be ambiguous*
• Uses bottom up Shift/Reduce parsing
– Get a token
– Push onto stack
– Can it reduced (How do we know?)
• If yes: Reduce using a rule
• If no: Get another token
• Yacc cannot look ahead more than one token
Phrase -> cart_animal AND CART
| work_animal AND PLOW
…
61. PLLab, NTHU,Cs2403 Programming
Languages
61
Shift/Reduce Conflicts
• shift/reduce conflict
– occurs when a grammar is written in
such a way that a decision between
shifting and reducing can not be made.
– ex: IF-ELSE ambigious.
• To resolve this conflict, yacc will
choose to shift.
62. PLLab, NTHU,Cs2403 Programming
Languages
62
YACC Declaration
Summary
`%start'
Specify the grammar's start symbol
`%union'
Declare the collection of data types that semantic values may
have
`%token'
Declare a terminal symbol (token type name) with no
precedence or
associativity specified
`%type'
Declare the type of semantic values for a nonterminal symbol
63. PLLab, NTHU,Cs2403 Programming
Languages
63
YACC Declaration
Summary
`%right'
Declare a terminal symbol (token type name) that is
right-associative
`%left'
Declare a terminal symbol (token type name) that is left-
associative
`%nonassoc'
Declare a terminal symbol (token type name) that is
nonassociative
(using it in a way that would be associative is a syntax error,
ex: x op. y op. z is syntax error)
64. PLLab, NTHU,Cs2403 Programming
Languages
64
Reference Books
• lex & yacc, 2nd Edition
– by John R.Levine, Tony Mason & Doug
Brown
– O’Reilly
– ISBN: 1-56592-000-7
• Mastering Regular Expressions
– by Jeffrey E.F. Friedl
– O’Reilly
– ISBN: 1-56592-257-3