2017-03-14 1 views
1

PythonでIf文をScalaで解析するにはどうすればよいですか?PythonのIf文をScalaで解析する

は、今のところ私はこのようなものを持っている:

val stmt: Parser[StatementStuff] = P ("\n".rep.? ~ "\t".rep.? ~ 
      (returnStatement | statement | assignStatement | 
      forLoop | blockBody | print | printString | 
      ifStatement) 
     ) 

私は、PythonのFizzbuzz解析することができるようにしたい:

import fastparse.all._ 

val ifStatement : P[Stmt] = P (
     ("if " ~ expr ~ ":" ~ ("\n".rep.? ~ "\t".rep.?) ~ stmt ~ ("\n".rep.? ~ "\t".rep.?) ~ "else:" ~ stmt).map { 
     case (e, st1, st2) => If (e, st1, st2) 
     } 
    ) 

のstmtは、基本的には、書類とValで

def main(): 
    count = 0 
    for count in range(1, 101): 
     if count % 5 == 0 and count % 3 == 0: 
      print "FizzBuzz" 
     elif count % 3 == 0: 
      print "Fizz" 
     elif count % 5 == 0: 
      print "Buzz" 
     else: 
      print count 
    count = count + 1 

今のようなif文を扱うことができます:

if x==0: 
    return poo 

しかし、私はのようなものを扱うしたいと思います: 場合は、カウント%5 == 0と数える%3 == 0:

すべてのヘルプは素晴らしいだろう、事前にあなたに感謝します!

パーサライブラリ:http://www.lihaoyi.com/fastparse/

+0

使用しているライブラリについて言及する価値があります。 –

+0

[最小、完全、および確認可能](https://stackoverflow.com/help/mcve)の何かを提供できますか? – Soapy

+0

import fastparse.all._を使用しています – yeny314

答えて

0

FASTPARSEは、構文解析のpythonで役立つかもしれないpythonParser、で更新されました。

関連する問題