lex: default rule for unrecognized strings - lex

In ocamllex, I can use _ as a lexer rule to match any string that does not match previously defined rules, and raise errors. How can achieve this in lex/flex?

Typically, you would define a rule like this, which would go at the very end:
.|\n { /* process default here */ }
This rule will match any character that wasn't matched by any other rule.
Hope this helps!

Related

FSharpLint, how to use the rule "InterfaceNamesMustBeginWithI" in SuppressMessageAttribute?

[<SuppressMessage("NameConventions","InterfaceNamesMustBeginWithI")>] //No effect
[<SuppressMessage("NameConventions","InterfaceNames")>] //It's working
module Test=
type [<AllowNullLiteral>] MutationEvent =
abstract attrChange: float with get, set
...
Also, failed to search source code about "InterfaceNamesMustBeginWithI".
The name of the rule is InterfaceNames, so you can suppress it thus:
[<SuppressMessage("","InterfaceNames")>]
module Test =
...
Also note that the first argument to SuppressMessage is not used by fsharplint, so it can be anything (although not null, strangely enough!)
There are pointers to InterfaceNamesMustBeginWithI in the documentation, but this is not correct.

Legal syntax for parameterized interface

I have a interface definition for a parameterized interface as follows :
interface A#(parameter adr=64,parameter data=128,parameter enable=1)(input clk, input rst);
endinterface
Now, can i declare something like ? Is this legal ?
A#(,,0) A0(clk,rst);
I did not find any such examples in the 1800-2012 SV LRM, so I was wondering about the legality of this syntax before I contact the EDA vendor. One such major tool vendor works perfectly fine but another major vendor complains about this - The error specifically being
near ",": syntax error, unexpected ','.
I don't know whether it's legal. Two simulators rejected it and one didn't (probably the same one as you). But you could
i) rearrange the order of the parameters:
interface A#(parameter enable=1, parameter adr=64,parameter data=128)(input clk, input rst);
endinterface
A #(0) a (.clk, .rst);
ii) use named mapping:
A #(.enable(0)) a (.clk, .rst);
You cannot skip over parameter overrides in an ordered list. It's illegal syntax according to the BNF:
list_of_parameter_assignments ::=
ordered_parameter_assignment { , ordered_parameter_assignment }
| named_parameter_assignment { , named_parameter_assignment }
ordered_parameter_assignment ::= param_expression
Contrast this with an ordered list of port connections:
list_of_port_connections29 ::=
ordered_port_connection { , ordered_port_connection }
| named_port_connection { , named_port_connection }
ordered_port_connection ::= { attribute_instance } [ expression ]
The param_expression in not optional in a parameter list, but an expression is optional in a port connection list.

Codeeffect Rule Engine - How to define rules to check length of string

I have to define a rule like <> length is < 5 or >5 AND not equal to 'N/A'.
I am not able to get Len() function in the list of operators for the string data type.
Please suggest if any field level attributes needs to define.
In your source class define a method like this:
public int Length(string str)
{
return string.IsNullOrEmpty(str) ? 0 : str.Length;
}
Then run your project, open the rule editor and create your rule like this:
Check if YourString has any value and Length(YourString) is greater than [5]
Details on in-rule methods can be found here

Exiting after executing a successful rule in Drools 6

I'm having an object as below:
class License{
private field1;
private field2;
private boolean active;
private String activeMessage;
private boolean processed = false;
//Getter and setter methods
}
What I'm trying to do is, based on the values of field1, and field2, I need to set the isActive flag and a corresponding message. However, if either the rule for field1 or field2 is fired, I need to stop the rules processing. That is, I need to execute only 1 successful rule.
I read on a post that doing ksession.fireAllRules(1) will solve this. But the fireAllRules() method is not available in Drools 6. I also tried putting a return; statement at the end of each rule. That didn't help me either.
Finally, I ended up adding an additional field to my object called processed. So whenever I execute any rule, I set the processed flag to true. And if the flag is already set, then I do not execute any rule. This is my rules file:
rule "Check field1"
when
$obj : License(getField1() == "abc" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
rule "Check field2"
when
$obj : License(getField2() == "def" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
However, I see that even now both my rules are being fired. When I try to print the value of isProcessed(), it says true, even though I enter the rule only if isProcessed() is false.
This is how I'm calling the drools engine:
kieService = KieServices.Factory.get();
kContainer = kieService.getKieClasspathContainer();
kSession = kContainer.newStatelessKieSession();
kSession.execute(licenseObj);
It is not just 2 rules, I have a lot of rules, so controlling the rules execution by changing the order of the rules in the drl file is not an option. What is happening here? How can I solve this problem? I am sort of new to Drools, so I might be missing something here.
Thanks.
Your question contains a number of errors.
It is definitely not true that fireAllRules has disappeared in Drools 6. You might have looked at the javadoc index, to find four (4!) overloaded versions of this method in package org.kie.api.runtime.rule in the interface StatefulRuleSession.
You might easily avoid the problem of firing just one out of two rules by combining the triggering constraint:
rule "Check field1 and field2"
when
$lic: License(getField1() == "abc" || getField2() == "def" )
//...
then
$lic.setXxx(...);
end
You complain that both of your rules fire in spite of setting the processed flag in the fact. Here you are missing a fundamental point (which is covered in the Drools reference manual), i.e., the necessity of notifying the Engine whenever you change fact data. You should have used modify on the right hand side of your rules.
But even that would not have been good enough. Whenever an update is made due to some properties, a constraint should be added to avoid running the update over and over again. You might have written:
rule "Check field1 and field2"
when
$lic: License(getField1() == "abc" || getField2() == "def",
! active )
//...
then
modify( $lic ){ setActive( true ) }
end
You might even write this in two distinct rules, one for each field, and only one of these rules will fire...

Xtext, Xbase: infer questions

I'm going to write an inferrer for my dsl and I have some questions that I could not solve with only the help of documentation.
First one:
I need to create static void main() from a feature: how can I add static with .toMethod()?
Second one:
Suppose I have a rule in my dsl like this:
Sequence:
'SEQ' name=ID '{'
statements+=Statement*
'}'
;
Where Statement:
Statement:
Sequence | others...
;
Sequence must be mapped to a void method and the body of that method is composed from the traslation of statements.
But the problem is this: when inside a Sequence I'll find another Sequence I have to create a method for the new sequence and add a call in super sequence.
For example:
SEQ first {
instructions...
SEQ second {
other instructions....
}
instructions...
}
Must generate:
void first(){
instructions(translated)...
second();
instructions(translated)...
}
void second(){
other instructions(translated)...
}
Is it possible to realize it?
First question:
ctx.toMethod('main', ctx.newTypeRef(Void.Type)) [
static = true
….
]
Second question:
Implying that with 'instructions' you mean instances of 'XExpression', you need to trick a bit, as it is currently not possible to compose new expressions during inference.
What you coudl basically do is to make your Sequence a subtype of XExpression. Then during JvmModelInference you need to walk over your expression tree (Statement) and create methods for Sequences. you need to override the XbaseTypeComputer as well as the XbaseCompiler and add type computation and compile strategies for your expressions. The latter would be a call to the method created during inference.