{"id":275,"date":"2008-05-15T10:41:41","date_gmt":"2008-05-15T10:41:41","guid":{"rendered":"http:\/\/blog.cyberwizzard.nl\/?p=275"},"modified":"2016-03-17T21:24:21","modified_gmt":"2016-03-17T20:24:21","slug":"matlab-grammar","status":"publish","type":"post","link":"https:\/\/blog.cyberwizzard.nl\/?p=275","title":{"rendered":"Matlab grammar"},"content":{"rendered":"<p>I described in a previous post how I was trying to parse Matlab code. I&#8217;ve given up on this endeavour because it is way too much work (we designated a Matlab compiler as a last resort to get everything stand-alone, we will require Matlab in our project to speed up development).<\/p>\n<p>I will however provide my incomplete and broken grammar, as promised. I hope somebody can use this later on as we have dropped the compiler approach completely.<\/p>\n<p>On a side note, we figured out why nobody has a complete grammar for Matlab: its too darn difficult &#8211; if you manage to somehow describe the syntax, implementing it (type checking and internal Matlab functions for example) will be a heck of a lot of work. But it can be done, that much is obvious by now. <\/p>\n<p>matlab.g<\/p>\n<pre>\/\/ Simple grammar for interpreting Matlab files for the MDDP project.<br \/>\/\/ Written by Berend Dekens<br \/>\/\/<br \/>\/\/ Note: this part of the project is abandoned and will not be completed. The grammar is mostly working in Antlr except<br \/>\/\/ for some dodgy errors. If you find this usefull and\/or manage to fix the parser errors, please let me know so I can<br \/>\/\/ fix the problems.<br \/>\/\/<br \/>\/\/ Known limitations:<br \/>\/\/ - No function calls without parenthesis<br \/>\/\/\tMatlab allows function calls in the form of 'function_name arguments'. This is annoying and thus not allowed.<br \/>\/\/ - No functions calls (period)<br \/>\/\/\tCurrently we do not allow function calls at all. Implementing this means supporting a large portion of basic<br \/>\/\/\tMatlab functions and support for declaring new functions across files. This is beyond the scope of this project.<br \/>\/\/ - No characters or strings in variables<br \/>\/\/\tOur application is matrices and vectors (integers). Boolean logic is included for the sake of logic blocks and loops.<br \/>\/\/<br \/>grammar simplematlab;<br \/>options {<br \/>\toutput=AST;<br \/>\tbacktrack=true;<br \/> }<br \/><br \/>\/\/ Grammar rules below, start the grammar with a list of statements<br \/>statementList<br \/>    :   statement (lineSep+ statementList? )?<br \/>    ;<br \/>    <br \/>lineSep<br \/>   :    ';' | '\\n' | ','<br \/>   ;<br \/><br \/>statement<br \/>    :   'if' parExpression statementList ( lineSep 'elseif' parExpression statementList)* ('else' statementList)? lineSep 'end'<br \/>    |   'for' Identifier '=' (Identifier | integerLiteral) ':' (Identifier | integerLiteral) (':' (Identifier | integerLiteral))? statementList 'end'<br \/>    |   parExpression<br \/>    ;<br \/><br \/>\/\/ Expressions<br \/>parExpression<br \/>    :   '(' expression ')'<br \/>    | expression<br \/>    ;<br \/>    <br \/>expression<br \/>    :   conditionalOrExpression (assignmentOperator expression)?<br \/>    |   '(' conditionalOrExpression (assignmentOperator expression)? ')'<br \/>    ;<br \/>    <br \/>assignmentOperator<br \/>    :   '='<br \/>    |   '+='<br \/>    |   '-='<br \/>    |   '*='<br \/>    |   '\/='<br \/>    ;<br \/><br \/>conditionalOrExpression<br \/>    :   conditionalAndExpression ( '||' conditionalAndExpression )*<br \/>    ;<br \/><br \/>conditionalAndExpression<br \/>    :   equalityExpression ( '&&' equalityExpression )*<br \/>    ;<br \/><br \/>equalityExpression<br \/>    :   relationalExpression ( ('==' | '!=') relationalExpression )*<br \/>    ;<br \/>    <br \/>relationalExpression<br \/>    :   additiveExpression ( relationalOp additiveExpression )*<br \/>    ;<br \/>    <br \/>relationalOp<br \/>    :   '&lt;='<br \/>    |   '&gt;='<br \/>    |   '&lt;' <br \/>    |   '&gt;' <br \/>    ;<br \/><br \/>additiveExpression<br \/>    :   multiplicativeExpression ( ('+' | '-') multiplicativeExpression )*<br \/>    ;<br \/><br \/>multiplicativeExpression<br \/>    :   unaryExpressionNotPlusMinus ( ( '*' | '\/' ) unaryExpressionNotPlusMinus )*<br \/>    ;<br \/><br \/>unaryExpressionNotPlusMinus<br \/>    :   '~' unaryExpressionNotPlusMinus<br \/>    |   '!' unaryExpressionNotPlusMinus<br \/>    |   primary ('++'|'--')?<br \/>    ;<br \/><br \/>primary<br \/>    :   parExpression<br \/>    |   literal<br \/>    |   Identifier (identifierSuffix)?<br \/>    ;<br \/><br \/>literal <br \/>    :   integerLiteral<br \/>    |   booleanLiteral<br \/>    |   'null'<br \/>    ;<br \/><br \/>integerLiteral<br \/>    :   Digit+<br \/>    ;<br \/><br \/>booleanLiteral<br \/>    :   'true'<br \/>    |   'false'<br \/>    ;<br \/><br \/>identifierSuffix<br \/>    :   ('[' ']')+ '.' 'class'<br \/>    |   ('[' expression ']')+ \/\/ can also be matched by selector, but do here<br \/>    |   arguments<br \/>    ;<br \/><br \/>expressionList<br \/>    :   expression (',' expression)*<br \/>    ;<br \/><br \/>arguments<br \/>    :   '(' expressionList? ')'<br \/>    ;<br \/><br \/>Identifier <br \/>    :   Letter (Letter | Digit)*<br \/>    ;<br \/><br \/>Letter\t: 'a'..'z' | 'A'..'Z';<br \/>Digit\t: '0'..'9';<br \/><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I described in a previous post how I was trying to parse Matlab code. I&#8217;ve given up on this endeavour because it is way too much work (we designated a Matlab compiler as a last resort to get everything stand-alone, we will require Matlab in our project to speed up development). I will however provide [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[190,196,197,198,460,459],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-programming","tag-grammar","tag-matlab","tag-matlab-grammar","tag-mddp","tag-no_ok","tag-not_ok"],"views":4263,"_links":{"self":[{"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275"}],"version-history":[{"count":1,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions\/455"}],"wp:attachment":[{"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cyberwizzard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}