INTRODUCCIÓ:
En aquest exercici considerarem un llenguatge de programació amb expressións i instruccions. Les expressions són sobre els operadors +,-,*, i sobre operands naturals i variables enteres. Hi ha dos tipus d’instruccions:
Cada instrucció apareix en una nova línia. Aquí tenim un exemple de programa:
x = 3 y=3+x z=2 * (x+y) Print(z - y+x)
El nostre objectiu serà escanejar cada línia, identificar els elements bàsics que hi apareixen i retornar una llista amb aquests elements. Per exemple, donat l’string "z=2 * (x+y)", haurem de produïr la llista de parelles d’strings:
<"variable","z">, <"operator","=">, <"number", "2">, <"operator","*">, <"marker","(">, <"variable","x">, <"operator","+">, <"variable","y">, <"marker",")">
EXERCICI:
Implementeu una funció que, donat un string line amb una instrucció en el nostre llenguatge de programació, retorna un list<pair<string,string> > que representa la seqüència d’elements atòmics del llenguatge que hi apareixen. Aquesta és la capcelera:
// Pre: "line" conté una instrucció correcta del llenguatge. // Post: retorna la llista d'elements atòmics del llenguatge que apareixen a "line". list<pair<string,string> > scanner(string line);
Fixeu-vos que l’enunciat d’aquest exercici ja ofereix uns fitxers que haureu d’utilitzar per a compilar: Makefile, program.cpp, scanner.hpp, utils.hpp, utils.cpp. Us falta crear el fitxer scanner.cpp amb els corresponents includes i implementar-hi la funció scanner que hem explicat. Valdrà la pena que utilitzeu algunes de les funcions oferides a utils.hpp. Quan pugeu la vostra solució al jutge, només cal que pugeu un tar construït així:
tar cf solution.tar scanner.cpp
Entrada
L’entrada té una seqüència de linies, on cadascuna és una instrucció del llenguatge. Fixeu-vos en que el programa que us oferim ja s’encarrega de llegir aquesta entrada. Només cal que implementeu la funció abans esmentada.
Sortida
El programa dona com a sortida, per a cada linia, la llista d’elements atòmics que hi apareixen. Fixeu-vos en que el programa que us oferim ja s’encarrega d’escriure aquesta sortida. Només cal que implementeu la funció abans esmentada.
Input
a=3*2+1 Print(a-(7-a)-(2*a)) a=a+6-3 Print(a-(3*a)) b=a*a-(a*2) Print(4*a) Print(b-(2-4)) Print(3*b) b=7-4-(a*4) Print(a+3-b+(7-3))
Output
<"variable","a"> <"operator","="> <"number","3"> <"operator","*"> <"number","2"> <"operator","+"> <"number","1"> <"function","Print"> <"marker","("> <"variable","a"> <"operator","-"> <"marker","("> <"number","7"> <"operator","-"> <"variable","a"> <"marker",")"> <"operator","-"> <"marker","("> <"number","2"> <"operator","*"> <"variable","a"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"variable","a"> <"operator","+"> <"number","6"> <"operator","-"> <"number","3"> <"function","Print"> <"marker","("> <"variable","a"> <"operator","-"> <"marker","("> <"number","3"> <"operator","*"> <"variable","a"> <"marker",")"> <"marker",")"> <"variable","b"> <"operator","="> <"variable","a"> <"operator","*"> <"variable","a"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","*"> <"number","2"> <"marker",")"> <"function","Print"> <"marker","("> <"number","4"> <"operator","*"> <"variable","a"> <"marker",")"> <"function","Print"> <"marker","("> <"variable","b"> <"operator","-"> <"marker","("> <"number","2"> <"operator","-"> <"number","4"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"number","3"> <"operator","*"> <"variable","b"> <"marker",")"> <"variable","b"> <"operator","="> <"number","7"> <"operator","-"> <"number","4"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","*"> <"number","4"> <"marker",")"> <"function","Print"> <"marker","("> <"variable","a"> <"operator","+"> <"number","3"> <"operator","-"> <"variable","b"> <"operator","+"> <"marker","("> <"number","7"> <"operator","-"> <"number","3"> <"marker",")"> <"marker",")">
Input
Print( 4+7+( 6-8)-(8-2)) a=1- 1 Print((a-7* a)*(a*1)) b=a+1 b=3 Print(8+(7- 2)) b=b-3 Print((b-2-b)*(6-(5-a)+ (a-3-(4+9)))) Print ( 9) Print((3* 9+4-9*(5- a)) *(b-a*7))
Output
<"function","Print"> <"marker","("> <"number","4"> <"operator","+"> <"number","7"> <"operator","+"> <"marker","("> <"number","6"> <"operator","-"> <"number","8"> <"marker",")"> <"operator","-"> <"marker","("> <"number","8"> <"operator","-"> <"number","2"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"number","1"> <"operator","-"> <"number","1"> <"function","Print"> <"marker","("> <"marker","("> <"variable","a"> <"operator","-"> <"number","7"> <"operator","*"> <"variable","a"> <"marker",")"> <"operator","*"> <"marker","("> <"variable","a"> <"operator","*"> <"number","1"> <"marker",")"> <"marker",")"> <"variable","b"> <"operator","="> <"variable","a"> <"operator","+"> <"number","1"> <"variable","b"> <"operator","="> <"number","3"> <"function","Print"> <"marker","("> <"number","8"> <"operator","+"> <"marker","("> <"number","7"> <"operator","-"> <"number","2"> <"marker",")"> <"marker",")"> <"variable","b"> <"operator","="> <"variable","b"> <"operator","-"> <"number","3"> <"function","Print"> <"marker","("> <"marker","("> <"variable","b"> <"operator","-"> <"number","2"> <"operator","-"> <"variable","b"> <"marker",")"> <"operator","*"> <"marker","("> <"number","6"> <"operator","-"> <"marker","("> <"number","5"> <"operator","-"> <"variable","a"> <"marker",")"> <"operator","+"> <"marker","("> <"variable","a"> <"operator","-"> <"number","3"> <"operator","-"> <"marker","("> <"number","4"> <"operator","+"> <"number","9"> <"marker",")"> <"marker",")"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"number","9"> <"marker",")"> <"function","Print"> <"marker","("> <"marker","("> <"number","3"> <"operator","*"> <"number","9"> <"operator","+"> <"number","4"> <"operator","-"> <"number","9"> <"operator","*"> <"marker","("> <"number","5"> <"operator","-"> <"variable","a"> <"marker",")"> <"marker",")"> <"operator","*"> <"marker","("> <"variable","b"> <"operator","-"> <"variable","a"> <"operator","*"> <"number","7"> <"marker",")"> <"marker",")">
Input
a=36*87+ 50 b=63- (a-a-( 43-a))-99- ( 27-81) a=9+ (85-(100-a)) Print(2+ (41+b+ (b-b)-( 84+ 25+4*71))) a=38-(75+59)-19*44-(44- (5*29-97*44)) a=a-21-27-83*25-( a+(74+34*61-(b+ a+41*85))) a=(21-23)* 82-(a-(a-41) ) Print(a-85*43+ (a-a) - (b-a+(b+91) -(b+97)* (a-a))- 51+(98+(82+ 39-40*16- (b+44)))) Print(52+b) Print (42* 34+39-85) Print( 8+b-b) Print (40+64-(32-83)) Print(82-(32+a)+(a- (a+ b+(b-51) -(35-76-(94+ 6) -18))) ) b=b- b bb=b-a-a-80*( 73+35) +(a+15+(a-55-87)) bb=21-b*(bb-bb-(10+a) ) a= bb*82- (bb+26)*93 bb= b+86-a-(93-26*bb)- (26* 71- 9-(a-72-(a+89)))+(78+ 58-18) bb=63- ( a- 16)-(92-38) Print (55*76-13+( 61- 15)*(84+b) )
Output
<"variable","a"> <"operator","="> <"number","36"> <"operator","*"> <"number","87"> <"operator","+"> <"number","50"> <"variable","b"> <"operator","="> <"number","63"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","-"> <"variable","a"> <"operator","-"> <"marker","("> <"number","43"> <"operator","-"> <"variable","a"> <"marker",")"> <"marker",")"> <"operator","-"> <"number","99"> <"operator","-"> <"marker","("> <"number","27"> <"operator","-"> <"number","81"> <"marker",")"> <"variable","a"> <"operator","="> <"number","9"> <"operator","+"> <"marker","("> <"number","85"> <"operator","-"> <"marker","("> <"number","100"> <"operator","-"> <"variable","a"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"number","2"> <"operator","+"> <"marker","("> <"number","41"> <"operator","+"> <"variable","b"> <"operator","+"> <"marker","("> <"variable","b"> <"operator","-"> <"variable","b"> <"marker",")"> <"operator","-"> <"marker","("> <"number","84"> <"operator","+"> <"number","25"> <"operator","+"> <"number","4"> <"operator","*"> <"number","71"> <"marker",")"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"number","38"> <"operator","-"> <"marker","("> <"number","75"> <"operator","+"> <"number","59"> <"marker",")"> <"operator","-"> <"number","19"> <"operator","*"> <"number","44"> <"operator","-"> <"marker","("> <"number","44"> <"operator","-"> <"marker","("> <"number","5"> <"operator","*"> <"number","29"> <"operator","-"> <"number","97"> <"operator","*"> <"number","44"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"variable","a"> <"operator","-"> <"number","21"> <"operator","-"> <"number","27"> <"operator","-"> <"number","83"> <"operator","*"> <"number","25"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","+"> <"marker","("> <"number","74"> <"operator","+"> <"number","34"> <"operator","*"> <"number","61"> <"operator","-"> <"marker","("> <"variable","b"> <"operator","+"> <"variable","a"> <"operator","+"> <"number","41"> <"operator","*"> <"number","85"> <"marker",")"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"marker","("> <"number","21"> <"operator","-"> <"number","23"> <"marker",")"> <"operator","*"> <"number","82"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","-"> <"number","41"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"variable","a"> <"operator","-"> <"number","85"> <"operator","*"> <"number","43"> <"operator","+"> <"marker","("> <"variable","a"> <"operator","-"> <"variable","a"> <"marker",")"> <"operator","-"> <"marker","("> <"variable","b"> <"operator","-"> <"variable","a"> <"operator","+"> <"marker","("> <"variable","b"> <"operator","+"> <"number","91"> <"marker",")"> <"operator","-"> <"marker","("> <"variable","b"> <"operator","+"> <"number","97"> <"marker",")"> <"operator","*"> <"marker","("> <"variable","a"> <"operator","-"> <"variable","a"> <"marker",")"> <"marker",")"> <"operator","-"> <"number","51"> <"operator","+"> <"marker","("> <"number","98"> <"operator","+"> <"marker","("> <"number","82"> <"operator","+"> <"number","39"> <"operator","-"> <"number","40"> <"operator","*"> <"number","16"> <"operator","-"> <"marker","("> <"variable","b"> <"operator","+"> <"number","44"> <"marker",")"> <"marker",")"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"number","52"> <"operator","+"> <"variable","b"> <"marker",")"> <"function","Print"> <"marker","("> <"number","42"> <"operator","*"> <"number","34"> <"operator","+"> <"number","39"> <"operator","-"> <"number","85"> <"marker",")"> <"function","Print"> <"marker","("> <"number","8"> <"operator","+"> <"variable","b"> <"operator","-"> <"variable","b"> <"marker",")"> <"function","Print"> <"marker","("> <"number","40"> <"operator","+"> <"number","64"> <"operator","-"> <"marker","("> <"number","32"> <"operator","-"> <"number","83"> <"marker",")"> <"marker",")"> <"function","Print"> <"marker","("> <"number","82"> <"operator","-"> <"marker","("> <"number","32"> <"operator","+"> <"variable","a"> <"marker",")"> <"operator","+"> <"marker","("> <"variable","a"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","+"> <"variable","b"> <"operator","+"> <"marker","("> <"variable","b"> <"operator","-"> <"number","51"> <"marker",")"> <"operator","-"> <"marker","("> <"number","35"> <"operator","-"> <"number","76"> <"operator","-"> <"marker","("> <"number","94"> <"operator","+"> <"number","6"> <"marker",")"> <"operator","-"> <"number","18"> <"marker",")"> <"marker",")"> <"marker",")"> <"marker",")"> <"variable","b"> <"operator","="> <"variable","b"> <"operator","-"> <"variable","b"> <"variable","bb"> <"operator","="> <"variable","b"> <"operator","-"> <"variable","a"> <"operator","-"> <"variable","a"> <"operator","-"> <"number","80"> <"operator","*"> <"marker","("> <"number","73"> <"operator","+"> <"number","35"> <"marker",")"> <"operator","+"> <"marker","("> <"variable","a"> <"operator","+"> <"number","15"> <"operator","+"> <"marker","("> <"variable","a"> <"operator","-"> <"number","55"> <"operator","-"> <"number","87"> <"marker",")"> <"marker",")"> <"variable","bb"> <"operator","="> <"number","21"> <"operator","-"> <"variable","b"> <"operator","*"> <"marker","("> <"variable","bb"> <"operator","-"> <"variable","bb"> <"operator","-"> <"marker","("> <"number","10"> <"operator","+"> <"variable","a"> <"marker",")"> <"marker",")"> <"variable","a"> <"operator","="> <"variable","bb"> <"operator","*"> <"number","82"> <"operator","-"> <"marker","("> <"variable","bb"> <"operator","+"> <"number","26"> <"marker",")"> <"operator","*"> <"number","93"> <"variable","bb"> <"operator","="> <"variable","b"> <"operator","+"> <"number","86"> <"operator","-"> <"variable","a"> <"operator","-"> <"marker","("> <"number","93"> <"operator","-"> <"number","26"> <"operator","*"> <"variable","bb"> <"marker",")"> <"operator","-"> <"marker","("> <"number","26"> <"operator","*"> <"number","71"> <"operator","-"> <"number","9"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","-"> <"number","72"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","+"> <"number","89"> <"marker",")"> <"marker",")"> <"marker",")"> <"operator","+"> <"marker","("> <"number","78"> <"operator","+"> <"number","58"> <"operator","-"> <"number","18"> <"marker",")"> <"variable","bb"> <"operator","="> <"number","63"> <"operator","-"> <"marker","("> <"variable","a"> <"operator","-"> <"number","16"> <"marker",")"> <"operator","-"> <"marker","("> <"number","92"> <"operator","-"> <"number","38"> <"marker",")"> <"function","Print"> <"marker","("> <"number","55"> <"operator","*"> <"number","76"> <"operator","-"> <"number","13"> <"operator","+"> <"marker","("> <"number","61"> <"operator","-"> <"number","15"> <"marker",")"> <"operator","*"> <"marker","("> <"number","84"> <"operator","+"> <"variable","b"> <"marker",")"> <"marker",")">