The code I wrote seems to not be able to detect a function. I tried many edits but nothing seems to be working.
program : function-decl | decl | function-def
;
decl : kind var-list SEMICOLON
{
tok_type = "variable";
}
;
kind : KW_INT {integer = true; floatType = false;}
| KW_FLOAT {integer = false; floatType = true;}
;
var-list : ID varmany
{
tok_type = "variable";
t.check_token (tok_type, $1, line_no, bodyCheck, parameter);
}
;
varmany : /*empty*/ | varmany COMMA ID
{
tok_type = "variable";
t.check_token (tok_type, $3, line_no, bodyCheck, parameter );
}
;
function-decl : kind ID LPAR kind RPAR SEMICOLON
{
current_func = $2;
declaration = true;
parameter= false;
tok_type= "function";
t.check_token (tok_type, current_func, line_no, bodyCheck, parameter );
current_func ="";
}
;
function-def : kind ID LPAR kind ID RPAR body
{
current_func = $2;
paramName = $5;
declaration = false;
parameter= false;
tok_type= "function";
t.check_token (tok_type, current_func, line_no, bodyCheck, parameter );
tok_type = "variable";
parameter=true;
t.check_token(tok_type, paramName, line_no, bodyCheck, parameter);
current_func ="";
}
;
For example, for text input :
int main (int DUMMY) {
int x,y,z;
float p;
int main (int x){x = y;}
p = -z * (x/345+y*1.0) + - 300;
p = -z * (x/345+y*1.0) + -300;
while (p>=-(x+y)*3.45/6-z)
z = z + 3;
}
I get these error messages:
Local int variable y declared in line 3.
Local int variable z declared in line 3.
Local int variable x declared in line 3.
Local float variable p declared in line 5.
Local int variable main declared in line 6.
syntax error on line 6, matched: (
Local int variable x declared in line 6.
syntax error on line 6, matched: )
Your function_decl rule insists on exactly one parameter and does not allow for its name.
Related
I am trying to write a small parser with golang target, but not using visitors or walkers, but I am not able to find any sample code to build my parser upon.
For example, the following is the grammar code which I am trying to replicate with golang:
# Expr.g4:
grammar Expr;
#header {
}
#parser::members {
def eval(self, left, op, right):
if ExprParser.MUL == op.type:
return left * right
elif ExprParser.DIV == op.type:
return left / right
elif ExprParser.ADD == op.type:
return left + right
elif ExprParser.SUB == op.type:
return left - right
else:
return 0
}
stat: e NEWLINE {print($e.v);}
| ID '=' e NEWLINE {self.memory[$ID.text] = $e.v}
| NEWLINE
;
e returns [int v]
: a=e op=('*'|'/') b=e {$v = self.eval($a.v, $op, $b.v)}
| a=e op=('+'|'-') b=e {$v = self.eval($a.v, $op, $b.v)}
| INT {$v = $INT.int}
| ID
{
id = $ID.text
$v = self.memory.get(id, 0)
}
| '(' e ')' {$v = $e.v}
;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
ID : [a-zA-Z]+ ; // match identifiers
INT : [0-9]+ ; // match integers
NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS : [ \t]+ -> skip ; // toss out whitespace
And this is the python tester code for it:
# test_expr.py:
import sys
from antlr4 import *
from antlr4.InputStream import InputStream
from ExprLexer import ExprLexer
from ExprParser import ExprParser
if __name__ == '__main__':
parser = ExprParser(None)
parser.buildParseTrees = False
parser.memory = {} # how to add this to generated constructor?
line = sys.stdin.readline()
lineno = 1
while line != '':
line = line.strip()
istream = InputStream(line + "\n")
lexer = ExprLexer(istream)
lexer.line = lineno
lexer.column = 0
token_stream = CommonTokenStream(lexer)
parser.setInputStream(token_stream)
parser.stat()
line = sys.stdin.readline()
lineno += 1
Can anybody please post a sample golang code which is equivalent to the above python and inlined code?
I have a massive text file that I need to convert to an CSV file so I can import it to an MySQL database.
The text file looks like this:
Original text file
VL;1;1001;Productname 1;Description 1;2;MTR;METER;217883;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VA;2;4044773815245;V;
VA;3;0036453;V;
VL;1;1002;Productname 2;This is product decrtiption for 2 product;2;MTR;METER;140365;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VX;WEIGHT;7500
VX;VOLUME;3249
VX;DIMENSJON;57x57x1000
VA;2;4044773452884;V;
VA;3;0036479;V;
VL;1;1003;Productname 3;Description......;2;MTR;METER;1575;10000;20171006;1;010606;&10;PRODUCER1;;N;10000;;
VX;PDF;1003.pdf
VX;IMAGE;1003.png
VX;BASEINFO;http://127.0.0.1/1003/
VX;WEIGHT;20
VX;DIMENSJON;0x7x0
VX;UNSPSC;26121616
VA;2;7070613017149;V;
VA;3;1000116;V;
Wanted result
I will need to convert it into a CSV file that looks like this:
type; Productnumber; Productname; Description; measurement_unit; price_unit; price_unit_txt; price; crowd; price_date; status; block_number; discount_group; manufac; type; stocked; sales_package; discount; price_type; PDF; IMAGE; baseinfo; WEIGHT; VOLUME; dimensjon; UNSPSC; va_2; va_3;
1; 1001; Productname 1; Description 1; 2; MTR; METER; 217883; 10000; 20180402 1; 010206; &10; PRODUCER1; ; N; 10000; ; ; ; ; ; ; ; ; ; 4044773815245; 0036453;
1; 1002; Productname 2; Description 2; 2; MTR; METER; 140365; 10000; 20180402; 1; 010206; &10; PRODUCER2 ; N; 10000; ; ; ; ; ; 7500; 3249; 57x57x1000; ; 4044773452884; 0036479;
1; 1003; Productname 3; Description ABC 3; 2; MTR; METER; 1575; 10000; 20171006; 1; 010606; &10; PRODUCER3; ; N; 10000; ; ; 1003.pdf; 1003.png; http://127.0.0.1/1003/; 20; ; 0x7x0; 26121616; 7070613017149; 1000116;
Explanation of the original file
The fist product line is always starting with VL and then continue in this order:
type;Productnumber;Productname;Description;measurement_unit;price_unit;price_unit_txt;price;crowd;price_date;status;block_number;discount_group;manufac;type;stocked;sales_package;discount;price_type;
PDF is always on a new line starting with VX;PDF;
IMAGE is always on a new line starting with VX;IMAGE;
baseinfo is always on a new line starting with VX;BASEINFO;
WEIGHT is always on a new line starting with VX;WEIGHT;
VOLUME is always on a new line starting with VX;VOLUME;
dimensjon is always on a new line starting with VX;DIMENSJON;
UNSPSC is always on a new line starting with VX;UNSPSC;
va_2 is always on a new line starting with VA;2;
va_3 is always on a new line starting with VA;3;
Hope someone can help me out with this :)
a way possible (not the wole solution)
#!/bin/bash
awk -F';' '
function init() {
# formation line to print_line
line = vl pdf image baseinfo weight volume dimensjon unspsc va_2 va_3
# erase ^M (\r)
gsub( /\r/;"";line )
# print a block
print line
# initialisation variables
vl = pdf = image = baseinfo = weight = volume = dimensjon = unspsc = va_2 = va_3 = ";"
}
# head/title, note that "%12s" format with 12 characters width
BEGIN { printf ( "%12s; %s; %s; %s; %s; %s; %s; %s; %s; %s;","vl","pdf","image","baseinfo ","weight","volume","dimensjon","unspsc","va_2","va_3" ) }
/^VL/ { init(); ; vl = sprintf( "%12s; %s; %s; %s; ", $3, $4, $5, $6 ) }
/^VX;WEIGHT;/ { weight = sprintf( "%s; ", $3 )}
# .. another conditions
END { init() }
' file.dat # > outputfile.csv
for test:
cat << end > file.dat
VL;1;1001;Productname 1;Description 1;2;MTR;METER;217883;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VA;2;4044773815245;V;
VA;3;0036453;V;
VL;1;1002;Productname 2;This is product decrtiption for 2 product;2;MTR;METER;140365;10000;20180402;1;010206;&10;PRODUCER1;;N;10000;;
VX;WEIGHT;7500
VX;VOLUME;3249
VX;DIMENSJON;57x57x1000
VA;2;4044773452884;V;
VA;3;0036479;V;
VL;1;1003;Productname 3;Description......;2;MTR;METER;1575;10000;20171006;1;010606;&10;PRODUCER1;;N;10000;;
VX;PDF;1003.pdf
VX;IMAGE;1003.png
VX;BASEINFO;http://127.0.0.1/1003/
VX;WEIGHT;20
VX;DIMENSJON;0x7x0
VX;UNSPSC;26121616
VA;2;7070613017149;V;
VA;3;1000116;V;
end
ouput
vl; pdf; image; baseinfo ; weight; volume; dimensjon; unspsc; va_2; va_3;
1001; Productname 1; Description 1; 2; ;;;;;;;;;
1002; Productname 2; This is product decrtiption for 2 product; 2; ;;;7500; ;;;;;
1003; Productname 3; Description......; 2; ;;;20; ;;;;;
I'm working on these files to add a custom autoincrement value to each pos ticket generated in odoo v10:
Point_of_sale es el modulo
.. \addons\point_of_sale\static\src\js\ models.js
.. \addons\pos_ticket\static\src\xml\pos_ticket_view.xml
odoo code:
Model.js
order_id_ : function (){
var x = 1;
if(this.pos.order.id)
{
x = this.pos.order.id++;
}
else
{
x = x++;
}
function sequense(num)
{
var s = ""+ num;
while (s.length < 8)
{
s = "0" + s;
}
return s;
}
return sequense(x);
},
pos_ticket_view.xml
<t t-esc="order.order_id_()" / >
But when I run it like this I get this error because the variable isn't yet created:
Your if statement can't be processed if one of the earlier variables don't exist.
Instead of:
if(this.pos.order.id)
You should ensure the early variables exist, like so:
if(this.pos && this.pos.order && this.pos.order.id)
If pos or pos.order is not set, it should stop evaluating the if statement and you should no longer receive an error.
I'm actually doing my own shell.
I have done the following special characters:
int commande(int fin, int fout, char * com, char * param, int * bg){
// execute a command
(ex. ls –l)
int symbole;
char *mot;
pid_t pid;
symbole = parsing();
switch(symbole){
case 0: // NL
case 1: // ;
case 2: // &
case 3: // <
case 4: // >
case 5: // | (Here I have some issues when I try to redirect the output of a command).
(correspond à ctrl+D)
case 10:// Mot
default:
}
return;
}
But I have some issues to do the redirection of an output when it is piped " |", when I have two instructions that follow themselves. Indeed I have tried the following operations which have all worked:
>myShell ps > fich
>myShell ls -l | wc -l
But not this one:
>myShell ls -l | wc -l >file
here are the two cases specifically developped. I think that the issue is in the case 5 and not in the case 4 because the first command I tried worked (which I shew you above).
case 4: // SYMBOLE : >
if(output==0){
output=1;
execute=1;
for (l=0;l<10;l++){
eltsoutput[l]=eltsCommande[l];
}
}
break;
case 5: // SYMBOLE : |
//if(tube==0){
/*for (l=0;l<10;l++){
eltstube[l]=eltsCommande[l];
}*/
p2=fork();
if(p2==0){
if(tube==0){
freopen( "fichtmp", "w", stdout );
execvp(eltsCommande[0], eltsCommande);
}
return(0);
}
else{ if(background==0){ // SANS MOD BG ATTENDRE FIN FILS
waitpid(p2, NULL, 0);
}
tube=1;
execute=1;
}
break;
Can you help me finding a way to execute two commands at the same time with | and that allow their result to go to a file?
In my shell, the case one work in the case of a redirection with an instruction ";":
}else if(output==1){
close(1);
int filew = creat(eltsCommande[0], 0644);
execvp(eltsoutput[0], eltsoutput);
Maybe I should use this code to make it work?
Looking at the NetBSD /bin/sh source code, I see the following pipe implementation:
static int
sh_pipe(int fds[2])
{
int nfd;
if (pipe(fds))
return -1;
if (fds[0] < 3) {
nfd = fcntl(fds[0], F_DUPFD, 3);
if (nfd != -1) {
close(fds[0]);
fds[0] = nfd;
}
}
if (fds[1] < 3) {
nfd = fcntl(fds[1], F_DUPFD, 3);
if (nfd != -1) {
close(fds[1]);
fds[1] = nfd;
}
}
return 0;
}
This function is called by evalpipe with 2 file descriptors:
STATIC void
evalpipe(union node *n)
{
struct job *jp;
struct nodelist *lp;
int pipelen;
int prevfd;
int pip[2];
TRACE(("evalpipe(0x%lx) called\n", (long)n));
pipelen = 0;
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
pipelen++;
INTOFF;
jp = makejob(n, pipelen);
prevfd = -1;
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
prehash(lp->n);
pip[1] = -1;
if (lp->next) {
if (sh_pipe(pip) < 0) {
if (prevfd >= 0)
close(prevfd);
error("Pipe call failed");
}
}
if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
INTON;
if (prevfd > 0) {
close(0);
copyfd(prevfd, 0, 1);
close(prevfd);
}
if (pip[1] >= 0) {
close(pip[0]);
if (pip[1] != 1) {
close(1);
copyfd(pip[1], 1, 1);
close(pip[1]);
}
}
evaltree(lp->n, EV_EXIT);
}
if (prevfd >= 0)
close(prevfd);
prevfd = pip[0];
close(pip[1]);
}
if (n->npipe.backgnd == 0) {
exitstatus = waitforjob(jp);
TRACE(("evalpipe: job done exit status %d\n", exitstatus));
}
INTON;
}
evalpipe is called in a switch statement in evaltree as follows:
case NPIPE:
evalpipe(n);
do_etest = !(flags & EV_TESTED);
break;
... which is called by the infinite loop in evalloop, and percolates up the tree till it gets to the eval function. I hope this helps.
I have this awk code:
BEGIN {
valid_name[0] = "CEO"
valid_name[1] = "maffu"
valid_name[2] = "gerry"
valid_name[3] = "bob"
valid_name[4] = "cath"
valid_name[5] = "tom.tom.the.son.of.the.piper"
valid_name[6] = "Insuro_Corp"
valid_name[7] = "who-pays-the-piper"
valid_name[8] = "a_hat_at_a_party"
valid_name[9] = "do_dot_the_eyes"
valid_name[10]= "Kim_dot_COM"
valid_domain[0] = "InsuroCorp"
valid_domain[1] = "cs.otago"
valid_domain[2] = "gmail"
valid_domain[3] = "enron"
valid_domain[4] = "research.techies"
valid_domain[5] = "1st.national"
valid_extension[0] = "co.nz"
valid_extension[1] = "com.au"
valid_extension[2] = "co.uk"
valid_extension[3] = "co.us"
valid_extension[4] = "co.ca"
valid_extension[5] = "com"
valid_numeric[0] = "[139.80.81.50]"
valid_numeric[1] = "[127.0.0.0]"
valid_numeric[2] = "[139.80.32.68]"
valid_numeric[3] = "[255.255.25.255]"
invalid_name[0] = "-foo"
invalid_name[1] = "f--d"
invalid_name[2] = "_at_"
invalid_name[3] = "Top$"
invalid_name[4] = "tom/tom"
invalid_name[5] = ".com.au"
invalid_name[6] = "white space"
invalid_name[7] = " white-space"
invalid_domain[0] = "Insuro-Corp"
invalid_domain[1] = "cs_otago"
invalid_domain[3] = "100%"
invalid_domain[4] = "AT&T"
invalid_extension[0] = "ac.nz"
invalid_extension[1] = "edu.au"
invalid_extension[2] = "tv"
invalid_extension[3] = "com.us"
invalid_extension[4] = "edu"
invalid_numeric[0] = "139.80.81.50"
invalid_numeric[1] = "[1..2]"
invalid_numeric[2] = "[139-80-81-50]"
invalid_numeric[3] = "[1][2][3]"
}
function generate_invalid_e_mail_address() {
at = rand() < 0.3 ? "_at_" : rand() < 0.1 ? "" : "#"
dot = dot = rand() < 0.3 ? "_dot_" : rand() < 0.1 ? "" : "."
if (rand() < 0.5) {
name = valid_name[int(rand()*11)]
if (rand() < 0.3) {
numeric = invalid_numeric[int(rand()*4)]
print name at numeric
} else {
if (rand() < 0.5) {
domain = valid_domain[int(rand()*6)]
extension = invalid_extension[int(rand()*5)]
} else {
domain = invalid_domain[int(rand()*5)]
extension = valid_extension[int(rand()*6)]
}
print name at domain dot extension
}
} else {
name = invalid_name[int(rand()*8)]
if (rand() < 0.3) {
numeric = valid_numeric[int(rand()*4)]
print name at numeric
} else {
domain = valid_domain[int(rand()*5)]
extension = valid_extension[int(rand()*6)]
print name at domain dot extension
}
}
}
BEGIN {
print "maffu#cs.otago.ac.nz"
print "bob.gmail.com"
for (i = 0; i < 518; i++) generate_invalid_e_mail_address()
}
This program should generate email address test cases and put
them in a file called 'bad.data' with the command:
awk -f bad.awk >bad.data
Instead bad.data is created as an empty file because of these
errors:
awk: bad.awk:70: extension = invalid_extension[int(rand()*5)]
awk: bad.awk:70: ^ syntax error
awk: bad.awk:73: extension = valid_extension[int(rand()*6)]
awk: bad.awk:73: ^ syntax error
awk: bad.awk:76: print name at domain dot extension
awk: bad.awk:76: ^ unexpected newline or end of string
awk: bad.awk:84: extension = valid_extension[int(rand()*6)]
awk: bad.awk:84: ^ syntax error
awk: bad.awk:86: print name at domain dot extension
awk: bad.awk:86: ^ unexpected newline or end of string
This is the first awk code I have seen. How do I fix it?
It looks like extension is a keyword. Just replace all extensions to extension1 for example:
extension1 = invalid_extension[int(rand()*5)];