mismatched input 'MT_DVS' expecting SEMI_COLON when adding casting data type - apache-pig

I can't see any syntax problem in my code:
E1ECPRA = FOREACH E1ECPRA_BRUT GENERATE
IsStringNull(CD_SI,'') AS CD_SI,
IsStringNull(CD_EFS,'') AS CD_EFS,
IsStringNull(IDT_ETT_CTR,'') AS IDT_ETT_CTR,
IsStringNull(NO_PCI,'') AS NO_PCI,
IsStringNull(CD_DVS_ORI,'') AS CD_DVS_ORI,
IsStringNull(CD_NOR_CG,'') AS CD_NOR_CG,
IsStringNull(CD_BT,'') AS CD_BT,
IsStringNull(CD_CRV_CIA_ORI,'') AS CD_CRV_CIA_ORI,
IsStringNull(NO_STR_CPB,'') AS NO_STR_CPB,
IsStringNull(NO_STR_RPQ,'') AS NO_STR_RPQ,
MT_DVS AS (float) MT_DVS,
MT_CVE AS (float) MT_EUR,
IsStringNull(NO_CTR_MTR_FUS,'') AS NO_CTR_MTR_FUS,
IsStringNull(CD_PCEC,'') AS CD_PCEC,
IsStringNull(CD_TY_PCEC,'') AS CD_TY_PCEC,
IsStringNull(CD_ACT_CG,'') AS CD_ACT_CG,
IsStringNull(CD_APL_SRC,'') AS CD_APLI_SRC,
IsStringNull(CD_LET_CPT,'') AS CD_LET_CPT;
Or the program return this error in the line :
MT_DVS AS (float) MT_DVS,
mismatched input 'MT_DVS' expecting SEMI_COLON
Is there a synthax error here ?

(float)MT_DVS AS MT_DVS,
(float)MT_CVE AS MT_EUR,
Or
MT_DVS:float AS MT_DVS,
MT_CVE:float AS MT_EUR,

Related

How to generate XML directly within the parser using Java System.out.println

The input file that I want to parse consists of one or more rows and each row consists of a greeting (Hello or Greeting) followed by a person's first name. Here is a sample input:
Hello Roger
Greeting Sally
I want to create a parser that outputs XML. For the sample input I want the parser to generate this XML:
<messages>
<message>
<greeting>Hello</greeting>
<person>Roger</person>
</message>
<message>
<greeting>Greeting</greeting>
<person>Sally</person>
</message>
</messages>
I want the XML generated directly within the parser file (MyParser.g4) using Java System.out.println
Here is my lexer:
lexer grammar MyLexer;
GREETING : ('Hello' | 'Greeting') ;
ID : [a-zA-Z]+ ;
WS : [ \t]+ -> skip ;
EOL : [\n] ;
Here is my parser:
parser grammar MyParser;
options { tokenVocab=MyLexer; }
document: (message+ {System.out.println("<messages>" + $message.value + "</messages>");});
message returns [String value]: (GREETING ID {value = "<message><greeting>" + $GREETING.text + "</greeting><name>" + $ID.text + "</name></message>";}) ;
I ran ANTLR on the lexer and parser and then compiled the Java code that ANTLR generated. This resulted in the following error message.
MyParser.java:154: error: cannot find symbol
value = "<message><greeting>" + (((MessageContext)_localctx).GREETING!=null?((MessageContext)_localctx).GREETING.getText():null) + "</greeting><name>" + (((MessageContext)_localctx).ID!=null?((MessageContext)_localctx).ID.getText():null) + "</name></message>";
^
symbol: variable value
location: class MyParser
What am I doing wrong, please?
You forgot the $ before value, it must be: $value = "<message><greeting>" + ….
And you also want to print every message, so not:
message+ {System.out.println( … );}
which will print just once, but like this instead:
(message {System.out.println( … );})+
This ought to do it:
parser grammar MyParser;
options { tokenVocab=MyLexer; }
document
: {System.out.println("<messages>");}
( message EOL? {
System.out.println(" " + $message.value);
})+
{System.out.println("</messages>");}
EOF
;
message returns [String value]
: GREETING ID {
$value = "<message><greeting>" + $GREETING.text + "</greeting><name>" + $ID.text + "</name></message>";
}
;
Can be tested like this:
String source = "Hello Roger\nGreeting Sally";
MyLexer lexer = new MyLexer(CharStreams.fromString(source));
MyParser parser = new MyParser(new CommonTokenStream(lexer));
parser.document();

How to get the line of a token in parse rules?

I have searched everywhere and can't find a solution. I am new to ANTLR and for an assignment, I need to print out (using similar syntax that I have below) an error message when my parser comes across an unidentified token with the line number and token. The documentation for antlr4 says line is an attribute for Token objects that gives "[t]he line number on which the token occurs, counting from 1; translates to a call to getLine. Example: $ID.line."
I attempted to implement this in the following chunk of code:
not_valid : not_digit { System.out.println("Line " + $not_digit.line + " Contains Unrecognized Token " $not_digit.text)};
not_digit : ~( DIGIT );
But I keep getting the error
unknown attribute line for rule not_digit in $not_digit.line
My first thought was that I was applying an attribute for a lexer token to a parser rule because the documentation splits Token and Rule attributes into two different tables. so then I change the code to be:
not_valid : NOT_DIGIT { System.out.println("Line " + $NOT_DIGIT.line + " Contains Unrecognized Token " $NOT_DIGIT.text)};
NOT_DIGIT : ~ ( DIGIT ) ;
and also
not_valid : NOT_DIGIT { System.out.println("Line " + $NOT_DIGIT.line + " Contains Unrecognized Token " $NOT_DIGIT.text)};
NOT_DIGIT : ~DIGIT ;
like the example in the documentation, but both times I got the error
rule reference DIGIT is not currently supported in a set
I'm not sure what I am missing. All my searches turn up how to do this in Java outside of the parser, but I need to work in the action code (I think that's what it is called) in the parser.
A block like { ... } is called an action. You embed target specific code in it. So if you're working with Java, then you need to write Java between { and }
A quick demo:
grammar T;
parse
: not_valid EOF
;
not_valid
: r=not_digit
{
System.out.printf("line=%s, charPositionInLine=%s, text=%s\n",
$r.start.getLine(),
$r.start.getCharPositionInLine(),
$r.start.getText()
);
}
;
not_digit
: ~DIGIT
;
DIGIT
: [0-9]
;
OTHER
: ~[0-9]
;
Test it with the code:
String source = "a";
TLexer lexer = new TLexer(CharStreams.fromString(source));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.parse();
which will print:
line=1, charPositionInLine=0, text=a

dlm package - Error in optim(parm, logLik, method = method, ...) : L-BFGS-B needs finite values of 'fn'

require(dlm)
start.vals = c(0,0,0)
names(start.vals) = c("lns2_obs", "lns2_alpha", "lns2_beta")
buildTVP <- function(parm, x.mat){
parm <- exp(parm)
return( dlmModReg(X=x.mat, dV=parm[1], dW=c(parm[2], parm[3])) )
}
TVP.mle = dlmMLE(y=k[,1], parm=start.vals, x.mat=k[,2], build=buildTVP, hessian=T)
in this code, k[,1] and k[,2] are 2 stocks prices. on TVP.mle line I got
Error in optim(parm, logLik, method = method, ...) : L-BFGS-B needs finite values of 'fn' " error.
k file link: https://drive.google.com/open?id=1scLaKRpSdmp-1T9qTp_5cEcBFnWKDAus
I could not find my mistake. Could you help me please?

Error creating PDF with Zend (fontWithName)

I'm trying to generate a PDF file using Zend but I keep getting errors when trying to set the font.
Here is my code:
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font = new Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font,24)
->drawText("Hello world!",72,720);
$pdf->$page;
$pdf->save("example.pdf");
And this is the error:
Parse error: syntax error, unexpected 'fontWithName' (T_STRING), expecting variable (T_VARIABLE) or '$' in /Users/pawel/Sites/Zend/application/modules/default/controllers/IndexController.php on line 83
I think you can just remove new for the font declaration:
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$style = new Zend_Pdf_Style();
$style->setFont($font, 24);
$page->setStyle($style);
fontWithName is a static function and Zend_Pdf_Font is an abstract class.
See the documentation for example.
There is another problem:
Replace
$pdf->$page;
by
$pdf->pages[] = $page;

MonoRail Select Using Enum

I've been following this guide and coming up with my own concoction in order to use MonoRail's FormHelper.Select that is generated from an enum. So here's the Brail syntax:
${FormHelper.Select("user.Role", ${LS.EnumToPairs(Roles)}, {"value":"First", "text":"Second"})}
"LS" is just my own helper, which I've defined as follows:
public IEnumerable<Pair<int, string>> EnumToPairs(Type e)
{
IList<Pair<int, string>> pairs = new List<Pair<int, string>>();
foreach (int val in Enum.GetValues(e))
pairs.Add(new Pair<int, string>(val, Enum.GetName(e, val)));
return pairs;
}
Yet from this, despite being the correct syntax, I get the following error:
Node '$({ return Castle.MonoRail.Views.Brail.ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(self.GetParameter('LS'), 'EnumToPairs', (self.GetParameter('Roles'),)) })' has not been correctly
The source error doesn't help much unfortunately:
Line 15: output FormHelper.TextField("user.Role", {"class":"text-input full-width"})
Line 16: output """
Line 17: """
Line 18: output FormHelper.Select("user.Role", ${LS.EnumToPairs(Roles)}, {"value":"First", "text":"Second"})
Line 19: output """
Any ideas what I'm doing wrong here?
EDIT
Based on the answer given below, the solution was finally this:
${FormHelper.Select("user.Role", LS.EnumToPairs(Roles), {"value":"First","text":"Second"})}
Where Roles was PropertyBag["Roles"] = typeof(Role);
Try this:
${FormHelper.Select("user.Role", LS.EnumToPairs(typeof(Roles)), {"value":"First", "text":"Second"})}